用到了PIL包,注意导包时应导入:Pillow
核心代码如下
from PIL import Image
import os
path = r"C:\Users\dell\Desktop\111" # change this dirpath.
listdir = os.listdir(path)
newdir = os.path.join(path, 'split') # make a new dir in dirpath.
if (os.path.exists(newdir) == False):
os.mkdir(newdir)
for i in listdir:
if '.png' in i: # the format of zed img.
filepath = os.path.join(path, i)
print(filepath)
filename = i.replace('.png','')
uppath = os.path.join(newdir, filename)
downpath = os.path.join(newdir, filename) + "_down.png"
img = Image.open(filepath)
size = img.size
# 准备将图片横向切割成2张小图片
weight = int(size[0])
height = int(size[1] // 2)
for i in range(2):
box = (0, height* i , weight, height * (i + 1))
region = img.crop(box)
region.save(uppath+'_'+str(i)+'.png')
网友评论