How can programmers send the most special "I love you" expression in Python before 520

Summary: When it comes to Valentine's day and Chinese Valentine's day, many children and friends will encounter such a c...
1. What needs to be prepared
2. Basic ideas
3. Operation process
Advanced~
Summary: When it comes to Valentine's day and Chinese Valentine's day, many children and friends will encounter such a century problem - how to surprise their girlfriend / wife with a different Festival. Today, I'd like to share with you a unique way to express my love - to spell out the shape of the beloved with "I love you"! When it comes to Valentine's day and Chinese Valentine's day, many children and friends will encounter such a century problem - how to surprise their girlfriend / wife with a different Festival. You say send flowers, when the circle of friends row, all are sent flowers, female ticket: "no creativity!" , then die; You say to send a gift, if the gift is not satisfactory, female ticket: "you don't understand me at all!" , and pawn; You say go shopping and buy it, feel the empty pocket, and quietly put the idea aside Today, I would like to share with you a unique way to express my love - to spell out the shape of the beloved with "I love you"!

Finished product drawing!

what? Can't see what it looks like? Enlarge to Kangkang

(eyes full of love for your shape ~)

Do you want to make such a picture? So let's start now~

1. What needs to be prepared

1 photo of super invincible, lovely and greasy girl ticket! python (python 3.7 is used here) Required Library cv2: used to read photos PIL: used to generate new pictures Computers with good performance (if you haven't contacted python, you don't want / can't configure the environment, install cv2, PIL and other libraries, or the computer performance is not enough, it is strongly recommended to use Huawei cloud's ModelArts for development to directly solve various problems of environment configuration and computer performance ~)

2. Basic ideas

To achieve this effect, you need to first have a picture, extract its pixel information, and then assign it to a new picture full of text, you can get a new picture as shown in the above figure! No more nonsense, let's start next~

3. Operation process

1. Import the used library first (the functions of the two are described in 1)

import cv2 from PIL import Image, ImageDraw, ImageFont

2. Then start to declare the drawing method

Here are two parameters: the path of the original image and the text to be written
def draw(pic, draw_text):

3. Then read the picture

Use the imread method in cv2 to read the picture 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) # Tell the program, we will write on this picture next!

4. Declare some parameters of the drawing (these parameters are the best size I get through many experiments ~)

n = 10 # Read pixel interval m = 9 # Font size font_path = 'Where do you use fonts' # Path to font font = ImageFont.truetype(font_path, size=m) # Assign font information to font variable for subsequent use

5. Next is the most critical step! Write the text and color the image according to the source image

Through two for loops, locate the color block of each text in turn (the middle interval n is because the text is much larger than the pixel block, and it is impossible to arrange the text completely according to the position and color of each pixel) Use the text() method to fill in the colored text. Several parameters represent:
[j, i] -- Coordinate position draw_text[int(j / n) % len(draw_text)] -- Find out which text to write (such as the first few words in "I love you") fill=(img[i][j][2], img[i][j][1], img[i][j][0]) -- Represents the color of the corresponding position of the source image (three represent respectively RBG Color value) font -- It's obviously 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 the order of the sentence

6. Write completed, export picture

blank.save('img_' + pic) # Save the generated picture

7. Call our encapsulated methods~

Input image path and target text, run ~ a unique expression map will be finished! draw('1.jpg ', "I love you") Attach the full code (remember to change the font path to your own computer's)
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!")

Advanced~

Since we can make pictures one by one, why don't we connect them to make an expression video? Of course! The specific implementation methods and codes are as follows: [2020 Huawei cloud AI actual combat camp] 520! Make a unique video for TA~ Click here to learn about Huawei's new cloud technology for the first time~

18 May 2020, 04:00 | Views: 9956

Add new comment

For adding a comment, please log in
or create account

0 comments