Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Nine Palace Pictures by Python

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to make Nine Palace Lattice Picture by Python". In daily operation, I believe many people have doubts about how to make Nine Palace Lattice Picture by Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to make Nine Palace Lattice Picture by Python". Next, please follow the editor to study!

The corresponding code is as follows:

#-*-coding: utf-8-* -''from PIL import Imageimport sys# fills a picture into a square and cuts it into 9 pictures.' 'from PIL import Imageimport sys# fills a picture as a square def fill_image (image): width, height = image.size # Select the larger values in the length and width as the new_image_length = width if width > height else height # to generate a new picture [White background] new_image = Image.new (image.mode) (new_image_length, new_image_length), color='white') # paste the previous picture on the new one Center if width > height:# the width of the original picture is greater than the height Then the vertical dimension # (xPowery) tuple of the filled image represents the starting position of the pasted image above relative to the following image 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 image def cut_image (image): width Height = image.size item_width = int (width / 3) box_list = [] # (left, upper, right, lower) for i in range (0Jing 3): # double loop Generate 9 pictures based on the location of the original image for j in range (0P3): # print ((itemic 1) * item_width, (juni1) * item_width)) box = (jacked itemic widthbased on the location of the original image, (job1) * item_width (iTun1) * item_width) box_list.append (box) image_list = [image.crop (box) for box in box_list] return image_list# save def 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) so far The study on "how to make Nine Palace Pictures by Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report