Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PIL problems: failed in loading any font library and using unicode
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. from PIL import Image
  6. import ImageFont, ImageDraw
  7.  
  8. text = sys.argv[1]
  9. if not isinstance(text, unicode):
  10.     text = text.decode('gbk')
  11. filename = sys.argv[2]
  12.  
  13. image = Image.new("RGBA", (100, 100), (255,255,255))
  14. usr_font = ImageFont.truetype("simsun.ttc", 50)  #In fact, it can't load any font lib.
  15. d_usr = ImageDraw.Draw(image)
  16. d_usr = d_usr.text((10, 10), text, fill = "blue", font=usr_font) #error when text is Chinese
  17. image.save(filename)
  18.        
  19. #! /usr/bin/python
  20. # -*- coding: utf-8 -*-
  21.  
  22. import Image
  23. import ImageDraw
  24. import ImageFont
  25.  
  26. ttfont = ImageFont.truetype ('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 20)
  27. text = u'我能有乾酪?'
  28. image = Image.new ('RGB', (256, 128), 0xffffff);
  29. ImageDraw.Draw (image).text ( (20, 20), text, font = ttfont, fill = (0, 0, 0) )
  30. image.save ('chinese.jpg')