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 use Python to make Nine Miyagi Pictures

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use Python to make Nine Palace Pictures". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Principle

1. The Nine Miyagi Image Generator made by Python is used to package exe files. Users do not need to deploy and install the Python development environment to run the program locally to quickly generate the Nine Miyagi image.

2. Use the PIL library to constantly draw small areas, cut them off and save them into new small pictures.

Example

Suppose that the width and height of each lattice are w and h respectively, then the coordinates of the upper left corner and the lower right corner of the row row (counting from 0) and column col (counting from 0) are (col * w, row * h), (col * w + w, r * h + h), respectively.

#-*-coding: UTF-8-*-# divide a picture into nine Import tkinter as tkfrom PIL import Imageimport sys # first fill input image into square def fill_image (image): width, height = image.size # choose larger values in length and width as 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') # pay attention to this function! # paste the previous image on the new image, centered if width > height:#, the width of the original image is greater than the height, then the vertical dimension of the filled image # (xMagery y) indicates the starting position of the pasted image above relative to the following image, which is a coordinate point. 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 # split the image def cut_image (image): width, height = image.size item_width = int (width / 3) # because there are 3 pictures in one row in moments. Box_list = [] # (left, upper, right, lower) for i in range (0Power3): for j in range (0Power3): # print ((itemic 1) * item_width, (juni1) * item_width)) box = (junitil) * item_width width, (job1) * item_width (iTun1) * item_width) box_list.append (box) image_list = [image.crop (box) for box in box_list] return image_list # Save the picture def save_images (image_list): index = 1 for image in image_list: image.save (str (index) + '.png', 'PNG') index + = 1 # Click the button Implement image segmentation def cTofClicked (): file_path=str (entryCd.get ()) # get the image path to be segmented image = Image.open (file_path) # image.show () image = fill_image (image) image_list = cut_image (image) save_images (image_list) labelcTof.config (text= "the picture has been born, please check it in the directory where the program is located!") # form top=tk.Tk () top.title ('Nine Palace Picture Generator') labelcTof=tk.Label (top,text= "Please enter the image path to be converted:", height=4,\ width=40,fg= "blue") labelcTof.pack () entryCd=tk.Entry (top,text='0') # text box, get the picture path entryCd.pack () label_tip=tk.Label (top,text= "Please check whether the picture path is entered correctly!" , height=2,\ width=40,fg= "gray") label_tip.pack () btnCal=tk.Button (top,text= "Click to generate Nine Miyagi Pictures", fg= "red", bg= "yellow", command=cTofClicked) # Click the callback function btnCal.pack () top.mainloop () # execute the main cycle "how to make Nine Miyagi Pictures with Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report