当前位置 : 主页 > 编程语言 > python >

1-2 图片批量裁剪

来源:互联网 收集:自由互联 发布时间:2021-06-25
# -*-coding:utf-8-*- # !/usr/bin/env python # Author :vilicute ‘‘‘ func:对某文件夹下的图片进行批量裁剪 ‘‘‘ import os import time from PIL import Imagestart = time.time()paths = " C:/Users/vilicute/Desktop/photo/IM
# -*-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))
网友评论