How do I add text to a picture using python?

Source: Internet
Author: User

This article and we share the main is to use Python in the picture to add text-related content, come together to see it, I hope to everyone Learn python helpful.

using OpenCV

adding text to a picture looks simple, but if you're using OpenCV to do but it is troublesome. There is no function to use a custom font file in OpenCV, which means that we cannot use our own font, and that it means that he cannot display Chinese characters. This is still a very deadly thing. and the text position he showed is not very well controlled. Like the following code, all he wants to do is show the number 3:

code:

#coding =utf-8

Import Cv2

Import NumPy as NP

from Pylab Import  *

%matplotlib Inline

Font=cv2. font_hershey_simplex# using the default font

Im=np.zeros ((50,50,3), np.uint8)# Create a new image, and be sure to uint8

Img=cv2.puttext (IM, ' 3 ', (0,40), font,1.2, (255,255,255), 2) # Add text,1.2 indicates font size, (0,40) is the initial position, (255,255,255) represents color, 2 for weight

Imshow (IMG)

results:

I can find the position of the text is not very good to grasp, the initial coordinate is the default point of the lower left corner of the coordinates, not very convenient. And after showing the text, we do not have a good grasp of his actual position and size.

but it's a little handy that we can change his weight without changing the font. This is the advantage of drawing without using PIL below.

using PIL

also in order to generate the number 3, the following is the use PIL the operation:

code:

Import Image,imagefont,imagedraw

Import NumPy as NP

From Pylab Import *

%matplotlib Inline

Font = Imagefont.truetype (' 3.ttf ', #) # Use a custom font, the second parameter represents the character size

im = Image.new ("RGB", (50,50)) # generate a blank image

Draw = Imagedraw.draw (IM) # Draw handle

X,y= (0,0) # The coordinates of the initial upper left corner

Draw.text ((x, y), ' 3 ', Font=font) # Drawing

Offsetx,offsety=font.getoffset (' 3 ') # Gets the offset position of the text

Width,height=font.getsize (' 3 ') # Get the file size

Im=np.array (IM)

Cv2.rectangle (IM, (offsetx+x,offsety+y), (Offsetx+x+width,offsety+y+height), (255,255,255), 1) #绘出矩形框

Imshow (IM

results:

we can find that PIL supports the use of custom font files and provides detailed information about where the fonts occupy, and we can pinpoint where the text occupies, especially useful in the application. The only disadvantage is that he cannot change the weight of the font (this is, after all, a font template).

In practice, it appears that the two methods should be used in merit.

Source: Myths 's personal blog

How do I add text to a picture using python?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.