|
楼主 |
发表于 2021-8-3 18:29:10
|
显示全部楼层
代码又改了一下,遍历多张图片,裁切完成
- import os
- from PIL import Image
- i = 1
- j = 2
- url = 'D:\py练习\pic' #定义图片文件夹
- for lujing,liebiao,wenjian in os.walk(url):
- for a in wenjian:
- srcPath = lujing +'\\' +a
- #print(srcPath)
- 定义存放裁切后图片路径,这里直接放在源文件夹下,也可以加一层目录
- dstPath = url + '\\'+f'{i}.png'
- dstPath1 = url + '\\'+f'{j}.png'
- # 读取图片
- img_1 = Image.open(srcPath)
- # 设置裁剪的位置
- crop_box = (0,0,img_1.size[0]/2,img_1.size[1])
- crop_box1 = (img_1.size[0]/2,0,img_1.size[0],img_1.size[1])
- i = i + 2
- j = j + 2
- # 裁剪图片
- img_2 = img_1.crop(crop_box)
- img_21 = img_1.crop(crop_box1)
- #保存图片
- img_2.save(dstPath)
- img_21.save(dstPath1)
- print(a,'裁切完成')
复制代码
|
|