from PIL import Image,ImageDraw,ImageFont # sample text and font unicode_text = "中文測是..." #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28, encoding="unic") #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='utf-8') #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='big5') #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='uni') #print(font) #print(font.) font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 40,encoding='big5') #font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 28,encoding='uni') #font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 28) #font = ImageFont.truetype("c:/windows/fonts/kaiu.ttf", 28,encoding='utf-8') # get the line size text_width, text_height = font.getsize(unicode_text) # create a blank canvas with extra space between lines canvas = Image.new('RGB', ( 500, 500), "orange") # draw the text onto the text canvas, and use black as the text color draw = ImageDraw.Draw(canvas) #text=u'距離除夕夜' #text = "\u9E9E" #text="\ufa53" #text = "\u4E2D\u6587" #u = u'\u6211' #text=u.encode('big5') text=u'\ua7da' #text=u"測試" #b_str = text.encode('big5') #b_str = b_str.decode('big5') def myunichchar(unicode_char): mb_string = unicode_char.encode('big5') try: unicode_char = unichr(ord(mb_string[0]) << 8 | ord(mb_string[1])) except NameError: unicode_char = chr(mb_string[0] << 8 | mb_string[1]) return unicode_char text=myunichchar('中')+myunichchar('文') draw.text((5,5), text, 'blue', font) # save the blank canvas to a file canvas.save("unicode-text.png", "PNG") canvas.show()