pillow模块Image.crop()函数切割图片方法,参数说明

使用Image.crop()方法对图片进行切割。

参数:

Image.crop(left, up, right, below)

left:与左边界的距离

up:与上边界的距离

right:还是与左边界的距离

below:还是与上边界的距离

简而言之就是,左上右下。

例子:将一张美女图片切分成9张(3*3)

图片:

 

美女.jpg

代码:

# -*- coding: utf-8 -*-

from PIL import Image

filename = r'路径\美女.jpg'
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))

切割出来的图片:

 

posted @ 2019-09-05 16:41  正态分个布  阅读(37101)  评论(1编辑  收藏  举报