Abstract:Every Valentine’s Day, qixi Festival, many friends will encounter the problem of the century — how to give your girlfriend/wife a special holiday surprise. Today, I will share a unique way to express my love — use “I love you” to spell out the appearance of the beloved one!


Every Valentine’s Day, qixi Festival, many friends will encounter the problem of the century — how to give your girlfriend/wife a special holiday surprise.

You say send a flower, when the time comes a delimit in friend circle, all send a flower of, female ticket: “have no originality!” And then died;

You say send a gift, if the gift that send is not satisfactory, female ticket: “you don’t understand me at all!” And died;

You say go shopping, feel your empty pockets, and just forget about it…


Today, I would like to share with you a unique way of expressing my love — using “I love you” to spell out the appearance of the beloved one!

The finished product figure!


What? Can’t see what it looks like? Zoom in kangkang

(Eyes full of the shape of love you ~)

Want to make a picture like this? So, let’s get started

1. There are things to prepare

Female ticket super invincible lovely greasy photo 1!

Python (python3.7 is used here)

Required libraries to use

Cv2: Used to read photos

PIL: Used to generate new pictures

The performance compares OK computer

(If you do not have access to Python, do not want/do not have the ability to configure the environment, install cV2, PIL and other libraries, or the computer performance is not enough, it is strongly recommended to use Huawei Cloud ModelArts development ~ directly solve the environment configuration, computer performance of various problems ~)

2. Basic idea

To achieve this effect, you first need to take a photo, extract its pixel information, and assign a new image to a row filled with text. The result is a new image like the one shown above.

Without further ado, let’s begin

3. Operation process

1. Import the libraries you use first (the roles of both are described in 1)

import cv2

from PIL import Image, ImageDraw, ImageFont


2. Then start declaring drawing methods

There are two parameters: the path to the original image and the text to write

def draw(pic, draw_text):


3. Then read the picture

Using the imread method in CV2 to read the image information,

Use the Image method in PIL to create a new Image with the same size as the source Image and a white background

img = cv2.imread(pic)

blank = Image.new(“RGB”, [img.shape[1], img.shape[0]], “white”)

DrawObj = ImageDraw.Draw(blank) #


4. Declare some parameters of the drawing (these parameters are the best size I have obtained through many trials ~)

N = 10 # Interval between reading pixels

M = 9 # font size

Font_path = ‘where you use font’ # font path

Font = imagefont. truetype(font_path, size=m) # Assign the font information to the font variable for later use


5. This is the most important step! Write the text and color the image according to the source image

Through two for loops, locate the color block where each text is in turn (the interval n is because the text is much larger than the pixel block, so it is impossible to arrange the text exactly according to the position and color of each pixel)

Use the text() method to fill in the colored text with the following parameters:

[j, I] — coordinate position

Draw_text [int(j/n) % len(draw_text)] —

The fill = (img [j] [2], [I] img [j] [1], [I] img [I] [j] [0]), on behalf of the source images corresponding to the location of the color (three represent the RBG color value)

Font – This is clearly the font information

for i in range(0, img.shape[0], n):

for j in range(0, img.shape[1], n):

drawObj.text(

[j, i],

draw_text[int(j / n) % len(draw_text)],

fill=(img[i][j][2], img[i][j][1], img[i][j][0]),

font=font

) # Fill in the text in sentence order


6. After the data is written, export the image

Blank. Save (‘img_’ + PIC) # Save the generated image


7. Call our encapsulated method ~

Enter the picture path and target text, run ~ a unique vindication picture is done!

Draw (‘1.jpg’, “I love you “)

Attach the full code (remember to change the font path to your own computer yo ~)

import cv2

from PIL import Image, ImageDraw, ImageFont



def draw(pic, draw_text):

img = cv2.imread(pic)

blank = Image.new(“RGB”, [img.shape[1], img.shape[0]], “white”)

drawObj = ImageDraw.Draw(blank)



n = 10

m = 9



font = ImageFont.truetype(font_path, size=m)



for i in range(0, img.shape[0], n):

for j in range(0, img.shape[1], n):

drawObj.text(

[j, i],

draw_text[int(j / n) % len(draw_text)],

fill=(img[i][j][2], img[i][j][1], img[i][j][0]),

font=font

)



blank.save(‘img_’ + pic)



Draw (‘1.jpg’, “I love you “)

In order to

Since you can make a picture frame by frame, why not connect them together to make a confession video?

Of course!

【2020 Huawei Cloud AI Combat camp 】520 to! Make a one-of-a-kind love video for your partner


Click to follow, the first time to learn about Huawei cloud fresh technology ~