fonttest.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from PIL import Image,ImageDraw,ImageFont
  2. # sample text and font
  3. unicode_text = "中文測是..."
  4. #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28, encoding="unic")
  5. #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='utf-8')
  6. #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='big5')
  7. #font = ImageFont.truetype("c:/portable/fonts/adv.ttf", 28,encoding='uni')
  8. #print(font)
  9. #print(font.)
  10. font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 40,encoding='big5')
  11. #font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 28,encoding='uni')
  12. #font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 28)
  13. #font = ImageFont.truetype("c:/windows/fonts/kaiu.ttf", 28,encoding='utf-8')
  14. # get the line size
  15. text_width, text_height = font.getsize(unicode_text)
  16. # create a blank canvas with extra space between lines
  17. canvas = Image.new('RGB', ( 500, 500), "orange")
  18. # draw the text onto the text canvas, and use black as the text color
  19. draw = ImageDraw.Draw(canvas)
  20. #text=u'距離除夕夜'
  21. #text = "\u9E9E"
  22. #text="\ufa53"
  23. #text = "\u4E2D\u6587"
  24. #u = u'\u6211'
  25. #text=u.encode('big5')
  26. text=u'\ua7da'
  27. #text=u"測試"
  28. #b_str = text.encode('big5')
  29. #b_str = b_str.decode('big5')
  30. def myunichchar(unicode_char):
  31. mb_string = unicode_char.encode('big5')
  32. try:
  33. unicode_char = unichr(ord(mb_string[0]) << 8 | ord(mb_string[1]))
  34. except NameError:
  35. unicode_char = chr(mb_string[0] << 8 | mb_string[1])
  36. return unicode_char
  37. text=myunichchar('中')+myunichchar('文')
  38. draw.text((5,5), text, 'blue', font)
  39. # save the blank canvas to a file
  40. canvas.save("unicode-text.png", "PNG")
  41. canvas.show()