运维开发网

1-2 图片批量裁剪

运维开发网 https://www.qedev.com 2020-07-11 18:12 出处:网络 作者:运维开发网整理
# -*-coding:utf-8-*- # !/usr/bin/env python # Author :vilicute ‘‘‘ func:对某文件夹下的图片进行批量裁剪
# -*-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))
0

精彩评论

暂无评论...
验证码 换一张
取 消