search
HomeBackend DevelopmentPython TutorialDetailed explanation of examples of adding Chinese characters to PILLOW pictures

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

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()

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 确定一个汉字的大小

4. Create a new canvas

draw = ImageDraw.Draw( img )

6 .Do whatever you want on the canvas

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

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
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
如何将windows 7的语言设置为中文如何将windows 7的语言设置为中文Dec 21, 2023 pm 10:07 PM

有些朋友可能会在安装系统时不小心设置成了英文,结果所有界面都变成了英文,看都看不懂。其实我们可以在控制面板中设置语言,将语言更改为中文,下面就一起来看一下更改的方法吧。win7如何更改语言为中文1、首先点击屏幕左下角的按钮,然后选择“ControlPanel”2、找到“Clock,Language,andRegion”下的“Changedispalylanguage”3、点击下方“English”就可以在下拉菜单中选择简体中文了。4、确定之后点击“Logoffnow”注销并重启电脑。5、回来之后

如何将Win10电脑的语言设置为汉语?如何将Win10电脑的语言设置为汉语?Jan 05, 2024 pm 06:51 PM

有时候我们再刚刚入手安装好电脑系统之后发现系统时英文的,遇到这种情况我们就需要把电脑的语言改成中文,那么win10系统里面该怎么把电脑的语言改成中文呢,现在就给大家带来具体的操作方法。win10电脑语言怎么改成中文1、打开电脑点击左下角的开始按键。2、点击左侧的设置选项。3、打开的页面选择“时间和语言”4、打开后,再点击左侧的“语言”5、在这里就可以设置你要的电脑语言。

怎么将eclipse语言设置为中文怎么将eclipse语言设置为中文Jan 04, 2023 pm 03:50 PM

eclipse语言设置为中文的方法:1、打开浏览器找到语言包下载地址,并将最新的安装包地址复制;2、打开eclipse,点击“help”,然后点击安装新的插件;3、点击“Add”,在Location中粘帖网址;4、在下拉菜单中找到简体中文包,进行勾选,点击Next等待安装;5、重启eclipse即可。

Win11系统语言如何改成中文Win11系统语言如何改成中文Jun 29, 2023 pm 01:15 PM

  Win11系统语言如何改成中文?近期有用户刚给电脑安装了最新的Win11系统,但是在使用中发现系统语言为英文,自己使用起来很吃力,为此有没有什么方法可以将系统语言改成中文呢?方法很简单,下面我们来看看这篇Win11系统语言设置为中文的方法吧。  Win11系统语言设置为中文的步骤  1、首先我们进入齿轮按钮的settings,然后找到其中的Time打开时间和语言。  2、在时间和语言中点击左边栏的Language选项,然后在右侧点击Addalanguage。  3、接着在上方搜索框输入chi

解决中文乱码问题的matplotlib方法解决中文乱码问题的matplotlib方法Jan 13, 2024 pm 02:49 PM

解决matplotlib中文乱码问题的方法,需要具体代码示例Matplotlib是一个常用的用于数据可视化的Python库,可以生成各种图表和图形。然而,对于中文用户来说,经常会遇到一个问题,就是生成的图表中的中文字符显示乱码。这个问题可以通过一些简单的方法来解决。本文将介绍一些常见的解决方法,并附上相关的代码示例,帮助读者解决这个烦人的问题。方法一:设置字

正确在matplotlib中显示中文字符的方法正确在matplotlib中显示中文字符的方法Jan 13, 2024 am 11:03 AM

在matplotlib中正确地显示中文字符,是很多中文用户常常遇到的问题。默认情况下,matplotlib使用的是英文字体,无法正确显示中文字符。为了解决这个问题,我们需要设置正确的中文字体,并将其应用到matplotlib中。下面是一些具体的代码示例,帮助你正确地在matplotlib中显示中文字符。首先,我们需要导入需要的库:importmatplot

介绍和使用Pillow库的主要功能介绍和使用Pillow库的主要功能Jan 13, 2024 am 09:14 AM

pillow库的主要功能介绍与使用方法概述:Pillow是一个非常常用的Python图像处理库,它是PythonImagingLibrary(PIL)的一个分支,提供了丰富的图像处理功能。Pillow支持各种图像格式的读写、基本的图像处理操作、图像转换、图像增强、图像合成等功能。本文将介绍Pillow库的各个主要功能,并提供具体的代码示例。安装Pillo

解决Ubuntu系统中WPS无法输入中文的问题该做什么?解决Ubuntu系统中WPS无法输入中文的问题该做什么?Dec 30, 2023 pm 12:55 PM

虽然Linux有LibreOffice,但是对微软的office兼容不是很好,有些排版会出现问题。而几年前,金山也开发了Linux版的WPS,不过在Ubuntu上使用,无法直接输入中文,这咋弄才可以让WPS正常输入中文呢1、打开WPS的文档,右上角的输入法已经是中文了,但是实际输入的时候,只能输入英文字母,出不了中文2、在终端输入:sudogedit/usr/bin/wps3、从第二行加上:exportXMODIFIERS="@im=fcitx"exportQT_IM_MODULE=&

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!