Introduction to the

We all know that in wechat circle of friends or Microblog and QQ dynamic, there are a lot of “obsessive-compulsive disorder patients” love to send 9 pictures, and some pictures are a picture divided into nine maps, for this operation, we know how to do it?

This article uses Python to do a grid picture generator, is a packaged EXE file, users do not need to deploy Python development environment, you can run this program locally, in order to quickly generate grid pictures.

Realize the principle of

The realization principle is very simple, that is to use PIL library to constantly draw small areas of the original picture and then cut off the storage into a new small picture.

If the width and height of each cell are w and h respectively, then the upper-left and lower-right coordinates of the first row (counting from 0) and the first col column (counting from 0) are (col * w, row * h) and (COL * w + W, r * h + h) respectively.

The source code

Here is all the code for the program, which is a Python GUI program with very little code and is easy to understand:

# -*- coding: UTF-8 -*-
# Divide an image into nine squares
import tkinter as tk
from PIL import Image 
import sys 
 
 
Fill the input image with a square
def fill_image(image) : 
 width, height = image.size 
 # Select the greater value of length and width as the new image
 new_image_length = width if width > height else height 
 # Generate new image [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 onto the new one, centered
 if width > height:# the original image is larger than the height, so the vertical dimension #(x,y) of the filling image represents the starting position of the pasted image 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 images
def cut_image(image) :
 width, height = image.size
 item_width = int(width / 3) # Because moments put 3 pictures in a row.
 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 images
def save_images(image_list) : 
 index = 1 
 for image in image_list: 
 image.save(str(index) + '.png'.'PNG') 
 index += 1 
 
 
Click the button to achieve image segmentation
def cTofClicked() :
 file_path=str(entryCd.get()) Get the path of the image 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="Nine grid picture has been born, please view in the program directory!")
 
# form
top=tk.Tk()
top.title('Sudoku Picture Generator')
labelcTof=tk.Label(top,text="Please enter the path of the image to be converted:",height=4,\
 width=40,fg="blue") 
labelcTof.pack()
entryCd=tk.Entry(top,text='0') # text box to get the image 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 a grid image",fg="red",bg="yellow",command=cTofClicked) Click the callback function
btnCal.pack()
 
top.mainloop() Execute the main loop
Copy the code

The results

Interface image

Enter the address of the picture in the text box and click “Click to generate grid picture”