Python can be used to add text to an image,

Source: Internet
Author: User

Python can be used to add text to an image,

This article mainly introduces two ways to add text in Images Using Python. The following describes how to share and process the text for your reference. Let's take a look at it.

1. Use OpenCV

Adding text to an image seems simple, but it is troublesome to use OpenCV. OpenCV does not use a function to customize the font file. This not only means that we cannot use our own font, but also means that we cannot display Chinese characters. This is a terrible thing. In addition, the text positions displayed by him are not well controlled. For example, in the following code, all he wants to do is to display the number 3:

Code:

# Coding = utf-8import cv2import numpy as npfrom pylab import * % matplotlib inlinefont = cv2.FONT _ HERSHEY_SIMPLEX # use default font im = np. zeros (50, 50, 3), np. uint8) # create an image. Note that uint8img = cv2.putText (im, '3', (1.2), font, 255,255,255, (), 2) # Add text, 1.2 indicates the font size, (255,255,255) indicates the initial position, () indicates the color, 2 indicates the thickness of imshow (img)

Result:

I can find that the position where the text appears is not easy to grasp. The initial coordinates are the coordinates in the lower left corner by default. After the text is displayed, we cannot grasp the actual position and size of the text.

However, we can change the font without changing the font. This is an advantage that PIL is not used for plotting.

Ii. Use PIL

To generate the number 3, the following operations are performed using PIL:

Code:

Import Image, ImageFont, ImageDrawimport numpy as npfrom pylab import * % matplotlib inlinefont = ImageFont. truetype ('3. ttf', 50) # use a Custom font. The second parameter indicates the character size im = Image. new ("RGB", (50, 50) # generate a blank image draw = ImageDraw. draw (im) # drawing handle x, y = (0, 0) # coordinate draw in the upper left corner. text (x, y), '3', font = font) # plot offsetx, offsety = font. getoffset ('3') # obtain the text offset position width, height = font. getsize ('3') # Get the object size im = np. array (im) cv2.rectangle (im, (offsetx + x, offsety + y), (offsetx + x + width, offsety + y + height), (255,255,255), 1) # imshow (im)

Result:

We can find that PIL supports the use of Custom font files and provides detailed information about the font position. We can precisely determine the position of the text, which is particularly useful in applications. The only disadvantage is that he cannot change the font width (after all, the font template is used ).

In actual application, it seems that the best choice of the two methods is required.

Summary

The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message, thank you for your support.

Related Article

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.