[問題] PIL 有時畫不出字體,疑字體檔問題
我嘗試使用 PIL 把字體檔繪製成圖片,然而有些字體可以繪出,有些則畫不出來。
似乎是字體檔本身的問題,不知道是哪方面的問題,以及如何修正?
程式碼如下,在命令列執行
image.py:
import Image, ImageDraw, ImageFont
size = 100
text = "人" # 畫出來看得到
font = "mingliu.ttc" # 字體檔 Windows 裡有
# text = "" # 換成這兩句就畫不出來了 (該字元為 U+E010)
# font = "hzcdp01b.ttf" # 字體檔: http://goo.gl/XiSw9
text = text.decode('UTF-8') # 確定正確安裝後此字體有 U+E010 這個字元
im = Image.new("RGBA", (size, size), "white")
draw = ImageDraw.Draw( im )
draw.ink = 0 + 0*256 + 0*256*256
font = ImageFont.truetype(font, size )
draw.text( (0,0), text, font=font )
data = im.getdata()
newData = list()
for item in data:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append((item[0], item[1], item[2], 255))
im.putdata(newData)
im.save( "text.png" )
另外順帶一問,我想要輸出透明背景的圖片,以上寫法可行,
但如果要畫白色字 (draw.ink = 255 + 255*256 + 255*256*256) 就會看不到
是否有其他更漂亮的替代寫法?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.36.142
※ 編輯: danny0838 來自: 114.42.36.142 (08/26 00:16)