当前位置: 移动技术网 > IT编程>脚本编程>Python > 1-2 图片批量裁剪

1-2 图片批量裁剪

2019年09月25日  | 移动技术网IT编程  | 我要评论

左巍巍,王一璠照片,阮庆兆

# -*-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))

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网