The corresponding codes are as follows.
# -*- coding: utf-8 -*-'''Fill an image with a square and then cut it to9 chartAuthor: WeChat Public： Big Data Frontier'''from PIL import Imageimport sys# Fill the image with a squaredef fill_image(image): width, height = image.size # Select the larger of the length and width as the new image's new_image_length = width if width > height else height # Generate a new image[ white background] new_image = Image. new(image.mode, (new_image_length, new_image_length), color='white') # Paste the previous image on the new one， centering if width > height:# The original image is wider than it is tall， then the vertical dimension of the filled image#(x,y) The binary represents the starting position of pasting the top figure relative to the bottom figure 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# cut a mapdef cut_image(image): width, height = image.size item_width = int(width / 3) box_list =  # (left, upper, right, lower) for i in range(0,3):# twofold cycle， generate9 The position of the image based on the original image for j in range(0,3): #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width)) 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# savedef save_images(image_list): index = 1 for image in image_list: image.save('./result/python'+str(index) + '.png', 'PNG') index += 1if __name__ == '__main__': file_path = "python.jpeg" image = Image. open(file_path) #image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list)
I believe that you know how to implement the code, the code may be difficult to understand the corresponding comments are also given, you quickly download the code to take a run.
>>1、Qiannan delegation participated in the Qianxing Shenzhen Thousands of miles to find Miao Matchmaking Conference2、Cat Chain Weekly NewsNo293、Little snapper python course study notes similarities and differences between lists tuples and strings4、Artificial intelligence machine learning desperately needs data and the more data there is the more accurate the results will be5、Liaocheng Special Session and Cybersecurity Forum of the Lake Minghu Talk series of public welfare activities was successfully held