相关疑难解决方法(0)

使用 PIL 绘制多语言文本并保存为 1 位和 8 位位图

我从这个不错的答案中的脚本开始。它适用于“RGB”,但 8 位灰度“L”和 1 位黑/白“1”PIL 图像模式只是显示为黑色。我究竟做错了什么?

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()
Run Code Online (Sandbox Code Playgroud)

输出: …

fonts python-imaging-library python-2.7

3
推荐指数
1
解决办法
1627
查看次数

标签 统计

fonts ×1

python-2.7 ×1

python-imaging-library ×1