看到一个好玩的方式,学习一下
思路
读取图片->填充图片为正方形(fill_image函数)->将图片切分为9张(cut_image函数)->保存图片(save_image)->over
代码
from PIL import Image import sys
def fill_image(image): width, height = image.size new_image_length = width if width > height else height new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white') if width > height: new_image.paste(image, (0, int((new_image_length - height) / 2))) else: new_image.paste(image, (int((new_image_length - width) / 2), 0)) return new_image
def cut_image(image): width, height = image.size item_width = int(width / 3) box_list = [] for i in range(0, 3): for j in range(0, 3): box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width) box_list.append(box) image_list = [image.crop(box) for box in box_list] return image_list
def save_images(image_list): index = 1 for image in image_list: image.save(str(index) + '.jpg') index += 1
if __name__ == '__main__': image_path = "img/mcat_jpg.jpg" img = Image.open(image_path) image = fill_image(img) image_list = cut_image(image)
|