-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ImageFont module: getsize is slow? #4651
Comments
You are using Windows, so I'm assuming your default Line 556 in a78de17
I would assume the advance value only depends on hinting, which should probably run without the FT_LOAD_RENDER flag. In fact, the FreeType docs mention the flag FT_LOAD_BITMAP_METRICS_ONLY which seems appropriate here and conflicts with FT_LOAD_RENDER . https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_load_xxx
The other potential improvement could be to preserve loaded glyphs between the different functions, as is recommended in the FreeType tutorial; I'm not sure how much of an impact this would have. |
I'm pretty sure I observed the same speed on MacOS. I wonder if this is different/faster on Linux distros? |
On Linux (and also Mac to a degree) it is more likely that you have Raqm installed; this is rare on Windows. The default behaviour if you don't explicitly set |
Testing with |
PR #4652 has been merged, and will be part of the next Pillow release, due out on July 1. |
Thanks for the quick update! |
@cristianocca the PR introduced by @nulano is advertised to improve speed by 15%. Is that sufficient, or do you think there is still more work that needs to be done here? |
Sorry I forgot to answer this. I think 15% is fair enough, and unless there's any other "obvious" improvement, it should be ok. I mean, it is still a bit slow in my opinion, but I guess the operation is by itself slow and nothing else can be done. |
To get size you have to make an image then get the size of text on that image; there is no way to get the text size and then make an image out of the return value. |
@millionhz Not quite. Text rendering is performed in four steps:
The |
@nulano |
Multiline text uses a constant line height; the current implementation uses the height of 'A' plus a user-specified pixel offset (4px by default). Monospace fonts have equal advance length (used in step 1), but the bounding box may vary (used in steps 2-3). The bounding box merely describes which area is covered by a glyph, not how much area should be skipped during layout. Additionally, AFAIK there is no way to know in advance that a font is monospace, this can only be determined from the results of step 1. |
@nulano What if i want to generate an image depending on the size of the text I am going to put in it. What is the procedure?
But this seems like the wrong way to do it. |
@millionhz There is also the identical function You may want to also call |
@cristianocca Pillow 8.0.0 has now been released adding |
What did you do?
See code sample.
What did you expect to happen?
getsize
should be fast. I'm trying to use this to implement a text-wrap mechanism (for SVGs), but the moment I add a single call to this in a for loop, timers go up 10 to 20 times slower.Calling
getsize
on small text multiple times adds a significant overhead. I wonder if it can be improved somehow, or it is really as good as it can get. For example, 50 calls takes about 50ms (with short text), and increases significantly the bigger the text.What actually happened?
getsize
is slow.What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: