Add Text on Image using PIL

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Add Text on Image using PIL



I have an application that loads an Image and when the user clicks it, a text area appears for this Image (using jquery), where user can write some text on the Image. Which should be added on Image.


jquery



After doing some research on it, I figured that PIL (Python Imaging Library ) can help me do this. So I tried couple of examples to see how it works and I managed to write text on an image. But I think there is some difference when I try it using Python Shell and in web environment. I mean the text on the textarea is very big in px. How can I achieve the same size of text when using PIL as the one on the textarea?


PIL


Python Shell



The text is Multiline. How can i make it multiline in image also, using PIL?


PIL



Is there a better way than using PIL? I am not entirely sure, If this is the best implementation.



html:


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



its the image being edited


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



the jquery to add the textarea.
Also the text area is position:absolute and fixed size.



Should i place it inside a form so i can get coordinates of textarea on image?
I want to write text on image when user clicks and save it on the image.





Why you want to write text on an Image using PIL (and I am Not sure if PIL helps in that). Isn't it good enough for you to show text in overlay, which is used most commonly in sliders.
– lalit
May 4 '13 at 14:18





I need it for a project i want the image to be saved. Pil can draw text on an image using ImageDraw, don't know if there is another way.
– Apostolos
May 4 '13 at 15:23





It will be helpful, If you can provide the Python code you are using?
– lalit
May 4 '13 at 17:20





not yet implemented in django. I tried to see how PIL works in interactive python console. Want to see if it works first and then transfer it in Django.
– Apostolos
May 4 '13 at 22:29




3 Answers
3



I think ImageFont module available in PIL should be helpful in solving text font size problem. Just check what font type and size is appropriate for you and use following function to change font values.


PIL


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



So your code will look something similar to:


from PIL import Image
from PIL import ImageFont
from 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 to put some extra effort to calculate font size. In case you want to change it based on amount of text user has provided in TextArea.


TextArea



To add text wrapping (Multiline thing) just take a rough idea of how many characters can come in one line, Then you can probably write a pre-pprocessing function for your Text, Which basically finds the character which will be last in each line and converts white space before this character to new-line.





That's my initial thought. So do i need my fonts to be in a specific folder in my webserver in order to work?I think yes. Wrapping text?is there a standard way or must i implement one?
– Apostolos
May 4 '13 at 22:28





You can keep the font file anywhere in your web server. Just make sure the path you provide is correct.
– lalit
May 5 '13 at 4:31





@lalit I tried your code on a Windows machine and I got an error for the font self.font = core.getfont(file, size, index, encoding) IOError: cannot open resource. How can I provide path to the font file?
– LWZ
Aug 21 '13 at 5:44


self.font = core.getfont(file, size, index, encoding) IOError: cannot open resource





@LWZ, To run the above code, just make sure that the font file is in the current directory in which you are executing above code. In case you are using it into your program just specify the full path for your file e.g. 'C:WindowsFontssans-serif.ttf'. Other thing which you need to make sure here is to give appropriate read permissions to the file.
– lalit
Aug 21 '13 at 6:44





font = ImageFont.truetype("./arial.ttf", 30);draw.textsize(msg, font=font);
– WeizhongTu
Jun 12 '15 at 3:25



You can make a directory "fonts" in a root of your project and put your fonts (sans_serif.ttf) file there. Then you can make something like this:


fonts_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fonts')
font = ImageFont.truetype(os.path.join(fonts_path, 'sans_serif.ttf'), 24)



Even more minimal example (draws "Hello world!" in black and with the default font in the top-left of the image):


...
from PIL import ImageFont
...
ImageDraw.Draw(
image # Image
).text(
(0, 0), # Coordinates
'Hello world!', # Text
(0, 0, 0) # Color
)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results