将大图片切割成小图 python 实现


import os
from PIL import Image

input_path = './train_yuan0823/'
outsrc_path = '.src/'
imglist1 = os.listdir(input_path)
imglist1.sort()
n = 0
f = open('./name.txt','w')
for img in imglist1:
    name = str(img)
    if name[-1] == 'g':
        print(name)
        f.write(name)
        f.write('\n')
        img = Image.open(os.path.join(input_path,name),'r')
        box1 = (0,0,713,713)
        im = img.crop(box1)
        im.save(os.path.join(outsrc_path,'%s.png'%n),'png')
        n += 1
        box2 = (311,0,1024,713)
        im = img.crop(box2)
        im.save(os.path.join(outsrc_path,'%s.png'%n),'png')
        n += 1
        box3 = (0, 311, 713, 1024)
        im = img.crop(box3)
        im.save(os.path.join(outsrc_path, '%s.png' % n), 'png')
        n += 1
        box3 = (311, 311, 1024, 1024)
        im = img.crop(box3)
        im.save(os.path.join(outsrc_path, '%s.png' % n), 'png')
        n += 1

 

 

你可能感兴趣的:(code,python)