|
@@ -0,0 +1,30 @@
|
|
|
+from PIL import Image,ImageDraw,ImageFont
|
|
|
+
|
|
|
+unicode_text = "這是一段中文測試"
|
|
|
+#font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 40,encoding='big5')
|
|
|
+#font = ImageFont.truetype("c:/portable/fonts/華康綜藝體.ttf", 40,encoding='big5')
|
|
|
+font = ImageFont.truetype("/var/fonts/adv.ttf", 40,encoding='big5')
|
|
|
+
|
|
|
+text_width, text_height = font.getsize(unicode_text)
|
|
|
+
|
|
|
+canvas = Image.new('RGBA', (500, 500), (255, 0, 0, 0) )
|
|
|
+
|
|
|
+draw = ImageDraw.Draw(canvas)
|
|
|
+
|
|
|
+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=''
|
|
|
+for c in unicode_text:
|
|
|
+ text+=myunichchar(c)
|
|
|
+#text=myunichchar('中')+myunichchar('文')+myunichchar('測')+myunichchar('試')
|
|
|
+draw.text((5,5), text, (255, 0, 0), font)
|
|
|
+
|
|
|
+# save the blank canvas to a file
|
|
|
+canvas.save("unicode-text.png", "PNG")
|
|
|
+canvas.show()
|