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 image” to select JPG/JPGE/PNG, and then click generate image. A note of note here: Whether you’re looking for a career in Python or a hobby, remember that project development experience is always at the core. If you don’t have the latest Python tutorial for 2020, you can add bjMSb07 on wechat to get it

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

At the encoding moment, 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).

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.

PIL passively maintained, a group of hobbyists forked PIL’s code and maintained and modified it on top of it. The forked code project is called Pillow.

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

Open the picture

img_raw = Image.open(img_path)

Loads an array of pixels for the image

Img_array = img_raw.load() copies the code and then creates a new canvas and sets 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)

Copying the code ends by looping img_RAW and assigning the values of the image pixel array to the font as the font 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 ((x, y), next(ch_gen), font=font, fill=img_array[x, y], direction=None)

Copying the code is done here.

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(‘ vlog ‘) self.window.geometry(‘300×200’) menubar = Menu(self.window) Duplicates the code Then you define two buttons and corresponding response methods.

Choose picture

Btn1 = Button(window, text=’ select image ‘, width=15, height=2, command=self.get_img) btn1.pack()

Generate images

Self. send_btn = Button(window, column =15, height=2, When the select image button is clicked, the get_img method is called with the following code.

def get_img(self): global IMGPATH

Select the file

filenames = askopenfilenames(filetypes=((“jpeg img”, “.jpeg”), (“jpg img”, “.jpg”), (“png img”, “*.png”))) if len(filenames) > 0: IMGPATH = fnlist else: FNSTR = [fn for fn in filenames] FNSTR = ‘\n’. Join (fnlist) self.l.config(text= FNSTR) # In the above code, the get_img method opens the system’s file selection box via tkinter’s askopenfilenames method. The filetypes parameter specifies the filetypes that can be selected. Eventually the method returns the full file path for the 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.

Finally, FOR the convenience of others, 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.

Make a 520 vindicate artifact in Python, worth collecting

Is that clear? One final note: Whether you’re looking for a career in Python or a hobby, remember that project development experience is always at the core. If you don’t have the latest Python tutorial in 2020, you can add the assistant wechat: bjMSb07 for free! Finally, thank you for reading, favorite favorites, forward. Thank you for your company. Remember to pay attention ^_^