欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

1-2 图片批量裁剪

程序员文章站 2022-05-18 21:14:47
# -*-coding:utf-8-*- # !/usr/bin/env python # Author :vilicute ''' func:对某文件夹下的图片进行批量裁剪 ''' import os import time from PIL import Image start = time.t... ......
# -*-coding:utf-8-*-
# !/usr/bin/env python
# author :vilicute
'''
func:对某文件夹下的图片进行批量裁剪
'''
import os
import time
from pil import image

start = time.time()

paths = "c:/users/vilicute/desktop/photo/img/"    # 读取文件路径
aim = "c:/users/vilicute/desktop/photo/ima/"      # 存放目标路径
print("正在裁剪...")

cnt = 0
for fname in os.listdir(paths):            # 遍历paths下的文件
    fpath = os.path.join(paths, fname)     # fpath为文件的绝对路径
    image = image.open(fpath)              # 打开图片
    width, height = image.size             # 获取图片尺寸
    # (left, upper, right, lower)          # 图片尺寸:width*height-->(width-8)*(height-8)
    box = (4, 4, width - 4, height - 4)    # 裁剪设置
    image = image.crop(box)                # 裁剪
    image.save(aim+"img_20190924_"+str(cnt)+".jpg") # 保存到指定路径(包括命名)
    cnt = cnt + 1

end = time.time()
print("裁剪完成!  time = "+str(end - start))