python - Add Text on Image using PIL -


i have application loads image , when user clicks it, text area appears image (using jquery), user can write text on image. should added on image.

after doing research on it, figured pil (python imaging library ) can me this. tried couple of examples see how works , managed write text on image. think there difference when try using python shell , in web environment. mean text on textarea big in px. how can achieve same size of text when using pil 1 on textarea?

the text multiline. how can make multiline in image also, using pil?

is there better way using pil? not entirely sure, if best implementation.

html:

<img src="images/test.jpg"/> 

its image being edited

var count = 0; $('textarea').autogrow(); $('img').click(function(){     count = count + 1;     if (count > 1){         $(this).after('<textarea />');         $('textarea').focus();     }    }); 

the jquery add textarea. text area position:absolute , fixed size.

should place inside form can coordinates of textarea on image? want write text on image when user clicks , save on image.

i think imagefont module available in pil should helpful in solving text font size problem. check font type , size appropriate , use following function change font values.

# font = imagefont.truetype(<font-file>, <font-size>) # font-file should present in provided path. font = imagefont.truetype("sans-serif.ttf", 16) 

so code similar to:

from pil import image pil import imagefont pil import imagedraw   img = image.open("sample_in.jpg") draw = imagedraw.draw(img) # font = imagefont.truetype(<font-file>, <font-size>) font = imagefont.truetype("sans-serif.ttf", 16) # draw.text((x, y),"sample text",(r,g,b)) draw.text((0, 0),"sample text",(255,255,255),font=font) img.save('sample-out.jpg') 

you might need put effort calculate font size. in case want change based on amount of text user has provided in textarea.

to add text wrapping (multiline thing) take rough idea of how many characters can come in 1 line, can write pre-pprocessing function text, finds character last in each line , converts white space before character new-line.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -