使用PIL绘制多语言文本并保存为1位和8位位图 [英] Drawing multilingual text using PIL and saving as 1-bit and 8-bit bitmaps

查看:320
本文介绍了使用PIL绘制多语言文本并保存为1位和8位位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从



imtype =L:



imtype =RGB:

解决方案

它看起来像TrueType字体不希望与任何小于RGB的工作。



您可以使用PIL的 .convert()方法尝试下载图像。
$ b

从RGB图像开始,它给出了:
$ b $ p $ image $ image $变换图像转换(L): code>



image.convert(1):



转换为8位灰度效果很好,但是以TrueType字体或任何字体这是基于灰度,一个1位的转换总是看起来很粗糙。

对于外观漂亮的1位图像,可能需要从1开始位数位中文字体,专为数字开/关显示设计。


I started with the script in this nice answer. It works just fine for "RGB", but the 8-bit gray scale "L" and 1-bit black/white "1" PIL image modes just appear black. What am I doing wrong?

from PIL import Image, ImageDraw, ImageFont
import numpy as np

w_disp   = 128
h_disp   =  64
fontsize =  32
text     =  u"你好!"

for imtype in "1", "L", "RGB":
    image = Image.new(imtype, (w_disp, h_disp))
    draw  = ImageDraw.Draw(image)
    font  = ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", fontsize)
    w, h  = draw.textsize(text, font=font)
    draw.text(((w_disp - w)/2, (h_disp - h)/2), text, font=font)
    image.save("NiHao! 2 " + imtype + ".bmp")
    data = np.array(list(image.getdata()))
    print data.shape, data.dtype, "min=", data.min(), "max=", data.max()

Output:

(8192,) int64 min= 0 max= 0
(8192,) int64 min= 0 max= 0
(8192, 3) int64 min= 0 max= 255

imtype = "1":

imtype = "L":

imtype = "RGB":

解决方案

It looks like the TrueType fonts do not want to work with anything less than RGB.

You can try down-converting the images using PIL's .convert() method.

Starting with the RGB image, this gives:

image.convert("L"):

image.convert("1"):

Converting to 8-bit gray scale works nicely, but starting with TrueType fonts, or any font that is based on a gray scale, a 1-bit conversion will always look rough.

For good looking 1-bit images, it is probably necessary to start with a 1-bit bitmapped Chinese font designed for digital on/off displays.

这篇关于使用PIL绘制多语言文本并保存为1位和8位位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆