uho*_*hoh 3 fonts python-imaging-library python-2.7
我从这个不错的答案中的脚本开始。它适用于“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)
输出:
(8192,) int64 min= 0 max= 0
(8192,) int64 min= 0 max= 0
(8192, 3) int64 min= 0 max= 255
Run Code Online (Sandbox Code Playgroud)
UPDATE:
This answer suggests using PIL's Image.point() method instead of .convert().
The whole thing looks like this:
from PIL import Image, ImageDraw, ImageFont
import numpy as np
w_disp = 128
h_disp = 64
fontsize = 32
text = u"??!"
imageRGB = Image.new('RGB', (w_disp, h_disp))
draw = ImageDraw.Draw(imageRGB)
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)
image8bit = imageRGB.convert("L")
imageRGB.save("NiHao! RGB.bmp")
image8bit.save("NiHao! 8bit.bmp")
imagenice_80 = image8bit.point(lambda x: 0 if x < 80 else 1, mode='1')
imagenice_128 = image8bit.point(lambda x: 0 if x < 128 else 1, mode='1')
imagenice_80.save("NiHao! nice 1bit 80.bmp")
imagenice_128.save("NiHao! nice 1bit 128.bmp")
Run Code Online (Sandbox Code Playgroud)
ORIGINAL:
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:
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.
| 归档时间: |
|
| 查看次数: |
1627 次 |
| 最近记录: |