Convert string to image in python -


i started learn python week ago , want write small programm converts email image (.png) can shared on forums without risking lots of spam mails.

it seems python standard libary doesn't contain module can i`ve found out there's pil module (pil.imagedraw).

my problem can't seem working.

so questions are:

  1. how draw text onto image.
  2. how create blank (white) image
  3. is there way without creating file can show in gui before saving it?

thanks :)

current code:

import image import imagedraw import imagefont  def getsize(txt, font):     testimg = image.new('rgb', (1, 1))     testdraw = imagedraw.draw(testimg)     return testdraw.textsize(txt, font)  if __name__ == '__main__':      fontname = "arial.ttf"     fontsize = 11        text = "example@gmail.com"      colortext = "black"     coloroutline = "red"     colorbackground = "white"       font = imagefont.truetype(fontname, fontsize)     width, height = getsize(text, font)     img = image.new('rgb', (width+4, height+4), colorbackground)     d = imagedraw.draw(img)     d.text((2, height/2), text, fill=colortext, font=font)     d.rectangle((0, 0, width+3, height+3), outline=coloroutline)      img.save("d:/image.png") 

  1. use imagedraw.text - doesn't formating, prints string @ given location

    img = image.new('rgb', (200, 100)) d = imagedraw.draw(img) d.text((20, 20), 'hello', fill=(255, 0, 0)) 

    to find out text size:

    text_width, text_height = d.textsize('hello') 
  2. when creating image, add aditional argument required color (white):

    img = image.new('rgb', (200, 100), (255, 255, 255)) 
  3. until save image image.save method, there no file. it's matter of proper transformation put gui's format display. can done encoding image in-memory image file:

    import cstringio s = cstringio.stringio() img.save(s, 'png') in_memory_file = s.getvalue() 

    this can send gui. or can send direct raw bitmap data:

    raw_img_data = img.tostring() 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -