Skip to content
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

Pillow dreprecation warning about ImageFont.getsize #163

Closed
brvoisin opened this issue Aug 1, 2022 · 13 comments
Closed

Pillow dreprecation warning about ImageFont.getsize #163

brvoisin opened this issue Aug 1, 2022 · 13 comments

Comments

@brvoisin
Copy link
Contributor

brvoisin commented Aug 1, 2022

Hello!

Pillow 9.2.0 is installed in my virtualenv and when I use python-barcode there is a deprecation warning about the method ImageFont.getsize.

virtualenv/lib/python3.9/site-packages/barcode/writer.py:441: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
    width, height = font.getsize(subtext)

Since the version 9.2, getsize is deprecated and replaced by getlength. See the PR python-pillow/Pillow#6381.

@WhyNotHugo
Copy link
Owner

Should we use ImageFont.getbbox or ImageFont.getlength here?

@brvoisin
Copy link
Contributor Author

brvoisin commented Aug 2, 2022

I think you should use getlength because it returns only the width and the height like getsize.

@nulano
Copy link

nulano commented Aug 2, 2022

Author of the font.getbbox and font.getlength functions and the deprecation PR here.

This is the code in question, yes?

def _paint_text(self, xpos, ypos):
font_size = int(mm2px(pt2mm(self.font_size), self.dpi))
font = ImageFont.truetype(self.font_path, font_size)
for subtext in self.text.split("\n"):
width, height = font.getsize(subtext)
# determine the maximum width of each line
pos = (
mm2px(xpos, self.dpi) - width // 2,
mm2px(ypos, self.dpi) - height,
)
self._draw.text(pos, subtext, font=font, fill=self.foreground)
ypos += pt2mm(self.font_size) / 2 + self.text_line_distance

And it seems you are trying to align text by the bottom-middle point?
For text alignment you should use font.getlength, but it doesn't give you the height.
I'd suggest using draw.text(..., anchor="ms") instead, see anchor documentation here.

Perhaps (untested):

 def _paint_text(self, xpos, ypos): 
     font_size = int(mm2px(pt2mm(self.font_size), self.dpi)) 
     font = ImageFont.truetype(self.font_path, font_size) 
     for subtext in self.text.split("\n"): 
         pos = ( 
             mm2px(xpos, self.dpi),
             mm2px(ypos, self.dpi),
         ) 
         self._draw.text(pos, subtext, font=font, fill=self.foreground, anchor="ms") 
         ypos += pt2mm(self.font_size) / 2 + self.text_line_distance 

@nulano
Copy link

nulano commented Aug 2, 2022

In general, getbbox should by used for styling the text (e.g. adding a background rectangle) and getlength should be used for layout (e.g. word wrapping, underline, text cursor). Text alignment should now be done using anchors instead (which internally uses the same value as getlength).

@WhyNotHugo
Copy link
Owner

That makes sense. This deprecation is a good opportunity to polish this a bit and align text the way it should be done.

@brvoisin Are you interested in writing a PR for this?

@WhyNotHugo
Copy link
Owner

Thanks for chiming in, @nulano !

@brvoisin
Copy link
Contributor Author

brvoisin commented Aug 4, 2022

@brvoisin Are you interested in writing a PR for this?

Yes, I'll write a PR soon.

Thanks @nulano for the solution.

brvoisin added a commit to brvoisin/python-barcode that referenced this issue Aug 4, 2022
python-barcode/barcode/writer.py:441: DeprecationWarning: getsize is
deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox
or getlength instead.
    width, height = font.getsize(subtext)

`ImageFont.getsize` is deprecated since the version 9.2 of Pillow. We
use anchor instead of computing the right position to have an aligned
text.

It is related to the issue WhyNotHugo#163.
The solution was suggested by nulano (contributor of Pillow).
@cedk
Copy link

cedk commented Jul 3, 2023

Pillow 10.0.0 has been released: https://pypi.org/project/Pillow/10.0.0/
It will be great to have a release with this fix.

@WhyNotHugo
Copy link
Owner

@cedk What fix? Pillow is unpinned, so 10.0.0 should work without any issues.

@cedk
Copy link

cedk commented Jul 4, 2023

This one otherwise the last release of python-barcode crashes with Pillow 10.0.0.

@cedk
Copy link

cedk commented Jul 5, 2023

For the record, our test suite is failing like this https://foss.heptapod.net/tryton/tryton/-/jobs/1990306 because the release 0.14.0 is missing this compatibility fix with Pillow 10.0.0.

@WhyNotHugo
Copy link
Owner

python-barcode 0.15.1 is out

@clevermiraz
Copy link

clevermiraz commented Aug 7, 2023

in new pillow 10.0.0 version remove some method and replace.

For More Info see their Doc: pillow documentation

or you can use pillow 9.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants