In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to write a love letter into a picture with Python3 code". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python3 code to write a love letter into a picture".
1 Pillow installation
We all know that the Pillow library will be used when operating on images, because Pillow is a third-party library, so we need to install it before using it.
The old rule, pip installation:
Pip install Pillow
Install by other means:
Python3 automatically installs third-party libraries to say goodbye to pip
Python: one line of code to import all Python libraries
2 introduction to Pillow
After the installation is complete, let's briefly introduce a few commonly used examples
2.2.1 create and save pictures
#-*-coding:utf-8-*-# @ Time: 2021-12-pictures @ Author: carl_DJfrom PIL import Image# create a new picture: mode is RGB,size: 520x520, color: 0100200img = Image.new ('RGB', (520520), (0100200)) # Save img.save (' img.jpg')
Running the code, you can see that an img.jpg picture is automatically created in the default path
Let's open the picture and see what it looks like.
Yes, this is the picture.
2.2 redraw the picture
We draw two lines in the picture generated above.
#-*-coding:utf-8-*-# @ Time: 2021-12-pictures @ Author: carl_DJfrom PIL import Image,ImageDraw# create a new image # img = Image.new ('RGB', (520520), (0100200)) # img.save (' img.jpg') # directly open the generated img.jpg image img = Image.open ('img.jpg') # to get the drawing object Need to draw a diagonal line draw = ImageDraw.Draw (img) # draw a diagonal line draw.line ((0Jing 0memimg.size [0], img.size [1]), fill = 128, width = 2) draw.line ((0memimg.size [1], img.size [0], 0), fill = 128, width = 2) # Save picture img.save ('img-line.jpg')
After running the code, we open the generated picture
Ness, it's in line with our expectations.
2.3 Image merging
Let's merge the two pictures.
#-*-coding:utf-8-*-# @ Time: 2021-12-pictures @ Author: carl_DJfrom PIL import Image# directly open the img.jpg picture img = Image.open ('img.jpg') # create a small picture img_small = Image.new (' RGB', (30d30), 'yellow') # paste the img_samll to the 20x20 location of the img.jpg picture img.paste (img_small) (20d20)) # Save the newly generated picture img.save ('img-paste.jpg')
Run the code, and let's see how it works.
Perfect.
3 actual combat
Let's briefly talk about our train of thought, which is roughly divided into three steps.
1. First create basic information: copy content, pixel size, font type, picture path.
2. Create a small image to fill the content, and fill the small image content in the state of creating a large image.
3. Loop through the inner pixel of the picture and paste the text on it.
Let's first take a look at the picture of the goddess of Tang San:
Let's look at the code implementation:
#-*-coding:utf-8-*-# @ Time: 2021-12-picture @ Author: carl_DJfrom PIL import Image,ImageDraw,ImageFont# pixel magnification img_king_size = 2 "input text ='I want to blow with you'# set font font = ImageFont.truetype ('simsun.ttc',img_king_size) # picture path img_path ='. / king.jpg'# Open the picture img = Image.open (img_path) img_w Img_h = img.size# create a small image Used to fill the font img_child = Image.new ("RGB", (img_king_size, img_king_size)) # to create a large image State filled small image font img_ans = Image.new ("RGB", (img_w * img_king_size,img_h * img_king_size)) # gets the width and height text_w of a single text Text_h = font.getsize ('middle') # text horizontal center offset_x = (img_king_size-text_w) > > vertical center text offset_y = (img_king_size-text_h) > > 1char_index = tiny image used to draw text draw = ImageDraw.Draw (img_child) # cycle through each pixel of the original image for x in range (img_w): for y in range (img_h): # draw the background To make the picture look better draw.rectangle ((0pc0rec) imgregninglighting size), fill = 'lightgray') # draw the font draw.text ((offset_x,offset_y), text [char _ index], font = font,fill = img.getpixel ((xMague y)) img_ans.paste (img_child, (x * img_king_size)) with the color values of the pixels at (XMague y) Y * img_king_size)) char_index = (char_index + 1)% len (text) img_ans.save ('king-letter.jpg')
Let's take a look at the effect picture.
Because the size of the generated image exceeds the maximum upload limit, the original image cannot be uploaded.
I'll just intercept it and see the effect:
I'll magnify it N times.
Thank you for your reading, the above is the content of "how to use Python3 code to write a love letter into a picture". After the study of this article, I believe you have a deeper understanding of how to use Python3 code to write a love letter into a picture, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.