Table of Contents
Index
Brief description
Preparation
Example
Rendering
Home Backend Development Python Tutorial Detailed explanation of examples of adding Chinese characters to PILLOW pictures

Detailed explanation of examples of adding Chinese characters to PILLOW pictures

May 18, 2018 pm 04:36 PM
pillow Chinese

Index

  • Brief description

  • Preparation

  • Example

  • Rendering

  • Conclusion

Brief description

I think when using opencv2 or 3 To add Chinese text to the picture, you need to download the Freetype library, compile it and link it to the opencv library to output Chinese. Most of the tutorials on inserting Chinese into pictures on the Internet are still win+vs configuration tutorials. For people like me who have an environment under win and an environment under linux, it is undoubtedly a bit troublesome to synchronize the code. Fortunately, there is an alternative, which is the example below: pillow.

Preparation

  1. pillow installation
    couldn’t be simpler, you can install it as follows:
    pip install pillow
    Or
    conda install pillow

  2. The next font library that supports Chinese
    Search keywords: ttf font. Download a font style library you like. The suffix of the file is generally ttf. I downloaded a Microsoft Yahei library. The file name is msyh.ttf

Example

First create a new python file: draw_chinese .py. For sample code ipython-notebook style, please go here
1. Import library

import cv2from PIL import Image,ImageFont,ImageDrawfrom matplotlib.pyplot import imshowimport numpy as np
Copy after login

Image: An instance of an object represents an image , you can perform some size transformation and affine transformation operations
ImageFont: Used to load the font library file downloaded in the preparation stage
ImageDraw: Based on the image object, create an An object that can draw lines and paste text on Image instances.
2. Create a picture

img = Image.new(mode="RGB",size=(400,150),color=(120,20,20))
#或者从numpy对象中创建也行。可以把opencv的图片转为numpy,通过numpy连接两个图像处理库。
#img = Image.fromarray(numpy_object)
img.show()
Copy after login

Detailed explanation of examples of adding Chinese characters to PILLOW pictures

3. Load the font library

path_to_ttf = r'data/msyh.ttf'
font = ImageFont.truetype(path_to_ttf, size=25)
#size 确定一个汉字的大小
Copy after login

4. Create a new canvas

draw = ImageDraw.Draw( img )
Copy after login

6 .Do whatever you want on the canvas

draw.text(xy=(30,30),text='Hello,南墙已破!',font=font)
img.show()
#当然也可以把这个写好字的图片转换回numpy
#img2array = np.asanyarray(img)
Copy after login

Rendering

Detailed explanation of examples of adding Chinese characters to PILLOW pictures

##Conclusion

This method does not require you to download and compile the Freetype library yourself. The installation of pillow is also very simple. The written code can be used in both python2 and python3. Cross-platform is fine too. It can be said that Curve has solved the problem that opencv does not natively support Chinese fonts.

pillow is a library that can do some of the work of opencv. So far, I have found a setting that is commonly used but does not exist in pillow. When drawing a rectangular frame, you cannot set the width of the rectangular frame edges. You need to implement it manually: each time based on the original Increase or decrease the coordinate position of a pixel to repeat the frame to achieve the effect of increasing the width. Opencv only needs to give a width parameter.

The above is the detailed content of Detailed explanation of examples of adding Chinese characters to PILLOW pictures. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

How to set Excel table to display Chinese? Excel switching Chinese operation tutorial How to set Excel table to display Chinese? Excel switching Chinese operation tutorial Mar 14, 2024 pm 03:28 PM

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHP Dompdf How to display Chinese characters correctly in PHP Dompdf Mar 05, 2024 pm 01:03 PM

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

An effective way to fix Chinese garbled characters in PHP Dompdf An effective way to fix Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 04:45 PM

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Will wwe2k24 have Chinese? Will wwe2k24 have Chinese? Mar 13, 2024 pm 04:40 PM

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

How to set the language of Windows 7 to Chinese How to set the language of Windows 7 to Chinese Dec 21, 2023 pm 10:07 PM

Some friends may accidentally set it to English when installing the system. As a result, all the interfaces are changed to English and they cannot be understood. In fact, we can set the language in the control panel and change the language to Chinese. Let’s take a look at how to change it. How to change the language in win7 to Chinese 1. First click the button in the lower left corner of the screen, and then select "Control Panel" 2. Find "Changedispalylanguage" under "Clock, Language, and Region" 3. Click "English" below to select from the drop-down menu Simplified Chinese. 4. After confirmation, click "Logoffnow" to log out and restart the computer. 5. After coming back

Tips for solving Chinese garbled characters when writing txt files with PHP Tips for solving Chinese garbled characters when writing txt files with PHP Mar 27, 2024 pm 01:18 PM

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

See all articles