Ideas from “Python programming time”, I improved on it, mainly through tkinter to write an interface for easy use, you can pay attention to “Python programming time” this conscience of the public account.

At the end of this article, you can download the packaged software without installing the Python environment and various dependencies. After decompressing the software, you can double-click the EXE file to use it.

Let’s take a look at the specific effects.

Run the program.

Click “Select picture” to choose JPG/JPGE/PNG any one of the three formats of the picture, and then click generate confession picture.

For example, IF I select a “attacking giant” image, the result will look like this.

Zoom in on the pictures and you’ll see that they’re all made up of “I like you! Composition.

Encoding time

We know that the picture is composed of different pixels, and each pixel has its own color, which is represented by RGB, as shown in the figure.

RGB itself is an array :(a,b,c). Each bit in the array ranges from 0 to 255. For example, black can be represented by (0,0,0).

I just need to take out the RGB value of each pixel in the picture and use this RGB value as the color of the font, so that the generated picture will still be the outline of the original image from a distance.

Now that you have a concrete idea, you can write code. The complete code is at the end of the article, but here are the key code snippets to understand the idea.

PIL can be used to manipulate images in Python.

Install the PIL first.

pip install pillow
Copy the code

PIL was passive maintenance, a group of hobbyists forked the PIL code and maintained and modified it on top of it. The forked code project was called Pillow.

First, open the Image with pil. Image and load the Image array with the load method

# Open image
img_raw = Image.open(img_path)
Load an array of pixels for the image
img_array = img_raw.load()
Copy the code

Then create a new canvas and set the font and font size to use to draw the image to be generated.

img_new = Image.new("RGB", img_raw.size, (0.0.0))
draw = ImageDraw.Draw(img_new)
font = ImageFont.truetype('SimHei.ttf', font_size)
Copy the code

The last step is to loop img_RAW and assign the corresponding values of the image pixel array to the font as its color.

for y in range(0, img_raw.size[1], font_size):
    for x in range(0, img_raw.size[0], font_size):
        Draw text and assign a color to it
        draw.text((x, y), next(ch_gen), font=font, fill=img_array[x, y], direction=None)
Copy the code

And we’re done.

Can we optimize it again?

In order to let the friends without programming experience can easily use, I decided to use Tkinter to make a simple interface, and then use pyInstaller to package, so that you do not need to install Python environment and PIL, directly out of the box.

Tkinter is a desktop application framework built into Python. It is lighter and less functional than PyQT5, but it is sufficient for developing widgets. Tkinter is built into Python and cannot be installed.

Here also gives part of the code, the complete code to see the end of the article.

Start by creating an interface.

self.window = window
self.window.title('Confession device')
self.window.geometry('300x200')
menubar = Menu(self.window)
Copy the code

Then you define two buttons and corresponding response methods.

# Select image
btn1 = Button(window, text='Select picture', width=15, height=2, command=self.get_img)
btn1.pack()

# generate images
self.send_btn = Button(window, text='Generate confession picture', width=15, height=2, command=self.gen_img)
self.send_btn.pack()
Copy the code

When the select image button is clicked, the get_img method is called as follows.

def get_img(self):
   global IMGPATH
   # select file
   filenames = askopenfilenames(filetypes=(("jpeg img"."*.jpeg"), ("jpg img"."*.jpg"), ("png img"."*.png")))
   if len(filenames) > 0:
       fnlist = [fn for fn in filenames]
       fnstr = '\n'.join(fnlist)
       self.l.config(text=fnstr) What file was selected
       IMGPATH = fnlist
   else:
       self.l.config(text='No image files are currently selected')
Copy the code

In the above code, the get_img method opens the system’s file selection box via tkinter’s askopenfilenames method, specifies the filetypes that can be selected with the filetypes parameter, and eventually returns the full file path for that file.

When you click generate image, the gen_img method will be called. This method is to call the method previously written to generate image.

The last

To make it easy for others to use, I packaged the program with PyInstaller. Windows friends can directly download, and then decompress the use, find the compressed file loveyou. Exe, double-click the operation can be used.

Note: the confession pictures will be generated in the same directory as loveyou.exe.

Link: pan.baidu.com/s/1y0633mzo… Concern “lazy programming” reply “confession” to obtain extraction code

The full code is also available on Github at github.com/ayuLiao/ILo…

If the article is helpful to you, click “watching”, let more people know me.