# python图像处理---切割 按几乘几来等分切割(crop()函数的运用)

from PIL import Image

im = Image.open('F:/test.jpg')
# 图片的宽度和高度
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))

left = 80
upper = 210
right = 505
lower = 640

region = im.crop((left,upper,right,lower))
region.save("F:/crop_test.jpg")

def crop(self, box=None):
"""
Returns a rectangular region from this image. The box is a
4-tuple defining the left, upper, right, and lower pixel
coordinate.

Note: Prior to Pillow 3.4.0, this was a lazy operation.

:param box: The crop rectangle, as a (left, upper, right, lower)-tuple.
:rtype: :py:class:~PIL.Image.Image
:returns: An :py:class:~PIL.Image.Image object.
"""

from PIL import Image

im = Image.open("F:/test.jpg")
# 图片的宽度和高度
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))

xx = 3
yy = 2
x = img_size[0] // xx
y = img_size[1] // yy
for j in range(yy):
for i in range(xx):
left = i*x
up = y*j
right = left + x
low = up + y
region = im.crop((left,up,right,low))
print((left,up,right,low))
temp = str(i)+str(j)
region.save("f:/test/"+temp+".png")