解决Python3用PIL的ImageFont输出中文乱码的问题

yipeiwu_com7年前Python基础

今天在用python3+ImageFont输出中文时,结果显示乱码

# coding:utf-8
from PIL import Image, ImageDraw, ImageFont

image= Image.new('RGB', (559, 320),(255,255,255))
draw = ImageDraw.Draw(image)

# draw.text()
font = ImageFont.truetype("arial", 40, encoding="unic") # 设置字体
draw.text((100, 50), "哈哈哈", 'black', font)
# del draw
image.show()
# printers = win32print.EnumPrinters(10)
# print printers

有个简单的解决方法,修改字体为simsun.ttc即可:

# coding:utf-8

from PIL import Image, ImageDraw, ImageFont

image= Image.new('RGB', (559, 320),(255,255,255))
draw = ImageDraw.Draw(image)

# draw.text()
font = ImageFont.truetype("simsun.ttc", 40, encoding="unic") # 设置字体
draw.text((100, 50), "哈哈哈", 'black', font)
# del draw
image.show()
# printers = win32print.EnumPrinters(10)
# print printers

以上这篇解决Python3用PIL的ImageFont输出中文乱码的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

通过实例学习Python Excel操作

通过实例学习Python Excel操作

这篇文章主要介绍了通过实例学习Python Excel操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.python 读取Exc...

python点击鼠标获取坐标(Graphics)

python点击鼠标获取坐标(Graphics)

使用Python进行图像编程,要使用到Graphics库。下面列举出较常用的代码 from graphics import * #设置画布窗口名和尺寸 win = Graph...

python内置数据类型之列表操作

 数据类型是一种值的集合以及定义在这种值上的一组操作。一切语言的基础都是数据结构,所以打好基础对于后面的学习会有百利而无一害的作用。   python内置的常用数据类型有:数字、字符串...

Python实现滑动平均(Moving Average)的例子

Python中滑动平均算法(Moving Average)方案: #!/usr/bin/env python # -*- coding: utf-8 -*- import nump...

numpy ndarray 取出满足特定条件的某些行实例

在进行物体检测的ground truth boxes annotations包围框坐标数据整理时,需要实现这样的功能: numpy里面,对于N*4的数组,要实现对于每一行,如果第3列和第...