How can I calculate the text size rendered by text method of an ImageDraw object in PIL?
I would like to draw multiline text with a maximum box width or height for example, being able to calculate the width from the text would help me to decide where to cut the text before writing it on the image.
You can do it using the ImageFont.getsize() method. As far as I know it will handle text with embedded newlines. See example of using it in this answer of mine to another question.
Related
I have some text paragraphs in Unicode.
What I want to do is to convert these paragraphs into images.
The process flow I chose was to convert the text into SVG first and then to png.
I am using python for this and do not use any third-party library at the moment as it's just simple text processing. (tspan element for each line)
The issue I face is some sentences are lengthy and go out of the view box.
Is there a way to figure out a consistent font size that would work for all paragraphs/images.?
Or in other words, is there a way to figure out the height and width in pixels of a given string?
Thanks
This question is related but uses javascript instead.
I want to create a synthetic dataset for character recognition from images. I have a list of words or sequences as text data that I want to draw as images with different fonts and font sizes.
I am using PIL python library for drawing text on images. But the problem is, when the text is drawn in different font sizes, I got stuck to get the coordinates of individual character for drawing the bounding box.
[![enter image description here][2]][2]
Any idea, code chunk or suggestion of any convenient resource would be very helpful.
so, as the topic says, I want to write some text on an transparent image.
Or, to be more specific, I want to write on multiple specific positions different text. The point is, I want to use a custom font (coolvetica) and (and there is my problem) I want it to be anti-aliased. unfortunately, PIL does not support font-anti-aliasing, as you might already know, but also see here:
PIL example image.
So as you see, PIL has not only an issue with the coordinates, but also does not really support font-anti-aliasing and adds some irritating black-ish border to the text. It is not very usable for me.
So after some searching, I found Python Wand. The output is a lot better, but the antialiasing leads not to the result I want: Wand example image. The curves are not as smooth as I want and there are some weird "pimples" like the dot at the end of the "r".
My code to use wand is this:
with Drawing() as draw:
with wandimage(filename="Skispringen_Score.png") as img:
draw.font_family = 'coolvetica.ttf'
draw.text_antialias = True
draw.fill_color = (Color("rgba(255, 255, 255, 255)"))
for postionName, scorePosition in scoreContent.items():
if isinstance(currentScoreText[postionName], int) and currentScoreText[postionName] > 9:
center = 2
else:
center = 0
draw.font_size = fontsize[scorePosition["size"]]
draw.text(scorePosition["x"] - center, scorePosition["y"], str(currentScoreText[postionName]))
draw(img)
img.save(filename='wand-image.png')
And here is an example of what the text should actually look like: PHP example image.
This image was generated with php and the text looks as smooth as I want to.
I also tried to use cairo, but the documentation is really not that good (especially examples are rare) and I just don't even know how to write any text with it nor how to set an custom font.
The actual output image has an resolution of 1280x720 and is - besides of the text - completely transparent, the image is just an overlay for an videostream.
Do you have any idea how to get a nice looking text onto the image with python?
You could try pycairo. Here are some code examples https://github.com/pygobject/pycairo/tree/master/examples
The reference docs for cairo are for C.
If you want even better results then you could render the image at 4 times the size and
downsample it to the desired size.
You can also try some filters like a gaussian blur.
For pixel perfect images you will have to use the same rendering engine that your php function is using.
It seems there's no out-of-the-box way to do this (I've skimmed the docs), but just in case: if I want to draw a sentence to an image with PIL but want control over individual words (or even individual letters), how can I do that?
I'm also open to solutions that use a library other than PIL.
The image below shows the kind of effect I'm going for.
There is no a direct way to do this. However, you are able to determine the text size - http://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.ImageDraw.textsize.
You could draw the first part of your text, get the size of that text to calculate the new position for the next part of text, and so on. That way, you could style each part of the text in any matter you see fit.
I already can add text to an image using Pillow in Python. However, I want to know how I can add formatted text. In particular, I want to add a box of text to an image such that the text is center justified.
If this isn't possible using Pillow, I am open to other image manipulation libraries (including in other languages) that make overlaying formatted text on images easier.
refer to the function in this link - http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.Draw.text
the first argument is location. you can give it based on the size of your image on which you want to add text.
Here is a simple library which does the job of text alignment using PIL:
https://gist.github.com/turicas/1455973