2017-11-29 12 views
1

私はthis nice answerでスクリプトを開始しました。 "RGB"ではうまく動作しますが、8ビットのグレースケール "L"と1ビットの黒/白 "1" PIL画像モードは黒く表示されます。私は間違って何をしていますか?PILを使用して多言語テキストを描画し、1ビットと8ビットのビットマップとして保存する

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

出力:

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

imtype = "1":enter image description here

imtype = "L":enter image description here

imtype = "RGB":enter image description here

答えて

2

更新:

This answerは、.convert()の代わりにPILのImage.point()メソッドを使用することを示しています。

全体のものは、次のようになりますTrueTypeフォントが何かで動作するようにしたくないように見えます

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

NiHao! RGBNiHao! 8 bitNiHao! 1 bit 80NiHao! 1 bit 128


ORIGINAL RGBより小さい。

PILの.convert()メソッドを使用して画像をダウンコンバートすることができます。 RGB画像で開始

が、これは得られる8ビットのグレースケールへの変換

image.convert("L"):enter image description here

image.convert("1"):enter image description here

はうまく機能するが、TrueTypeフォントで始まる、または任意のフォントグレースケールに基づいて、1ビットの変換は常に粗く見えます。

1ビットの画像を正しく表示するには、デジタルオン/オフ表示用に設計された1ビットのビットマップ中国語フォントから開始する必要があります。

関連する問題