Realization of image nine square segmentation by python

  • 2021-07-22 10:54:54
  • OfStack

As we all know, in the WeChat circle of friends or Weibo and QQ dynamics, many "obsessive-compulsive disorder patients" love to send 9 pictures, and some pictures are 9 pictures divided into 1 picture. Do you know how to do this operation?

This paper is a 9-grid picture generator made by Python, which is a packaged exe file. Users can run this program locally without deploying and installing Python development environment, so as to quickly generate 9-grid pictures.

The following is all the code of the program. This is an Python GUI program. There are not many codes and it is easy to understand:


# -*- coding: UTF-8 -*-
#  Will 1 A picture is divided into 9 Zhang, 9 Palace lattice 
import tkinter as tk
from PIL import Image 
import sys 
 
 
# First put  input image  Fill as a square  
def fill_image(image): 
 width, height = image.size 
 # Select the larger of length and width as the  
 new_image_length = width if width > height else height 
 # 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 picture on the new picture, center it  
 if width > height:# If the original image is wider than the height, fill the vertical dimension of the image  #(x,y)2 Tuples represent the starting position of pasting the above picture relative to the following picture , It's 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 a picture  
def cut_image(image):
 width, height = image.size
 item_width = int(width / 3) # Because of the circle of friends 1 Arrangement 3 A picture. 
 box_list = []
 # (left, upper, right, lower)
 for i in range(0,3):
 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
 
# Save Picture  
def save_images(image_list): 
 index = 1 
 for image in image_list: 
 image.save(str(index) + '.png', 'PNG') 
 index += 1 
 
 
#  Click the button to realize picture segmentation 
def cTofClicked():
 file_path=str(entryCd.get()) #  Gets the path of the picture 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="9 The picture of Gongge has been born, please view it in the directory where the program is located! ")
 
#  Form 
top=tk.Tk()
top.title('9 Miyagi picture generator ')
labelcTof=tk.Label(top,text=" Please enter the path of the picture to be converted: ",height=4,\
 width=40,fg="blue") 
labelcTof.pack()
entryCd=tk.Entry(top,text='0') #  Text box to 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 Generate 9 Palace picture ",fg="red",bg="yellow",command=cTofClicked) #  Click the callback function 
btnCal.pack()
 
top.mainloop() #  Execute the main loop 

Packaged exe program download address: python realizes picture 9 grid segmentation


Related articles: