PIL.Image.crop()
程序使用PIL.Image.crop()将一张图片切割成9张小图片。
show you the code:
# -*- coding: utf-8 -*-
from PIL import Image
filename = r'path of picture you want to crop'
img = Image.open(filename)
size = img.size
print(size)
# 准备将图片切割成9张小图片
weight = int(size[0] // 3)
height = int(size[1] // 3)
# 切割后的小图的宽度和高度
print(weight, height)
for j in range(3):
for i in range(3):
box = (weight * i, height * j, weight * (i + 1), height * (j + 1))
region = img.crop(box)
region.save('{}{}.png'.format(j, i))