pillow模塊Image.crop()函數切割圖片方法,參數說明

使用Image.crop()方法對圖片進行切割。spa

參數:code

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

left:與左邊界的距離orm

up:與上邊界的距離blog

right:仍是與左邊界的距離圖片

below:仍是與上邊界的距離utf-8

簡而言之就是,左上右下。io

例子:將一張美女圖片切分紅9張(3*3)form

圖片:class

 

美女.jpgimport

代碼:

# -*- 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))

切割出來的圖片:

相關文章
相關標籤/搜索