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

Fix deprecation warning about ImageFont.getsize #165

Merged
merged 2 commits into from
Aug 5, 2022

Conversation

brvoisin
Copy link
Contributor

@brvoisin brvoisin commented 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 #163.
The solution was suggested by @nulano (contributor of Pillow).

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).
@brvoisin
Copy link
Contributor Author

brvoisin commented Aug 4, 2022

I tested with this script test.py:

import sys

from barcode import generate
from barcode.writer import  ImageWriter

with open(sys.argv[1], "wb") as f:
    generate("CODE128", "My super test", writer=ImageWriter(), output=f)

The image is slightly different.
before
after

Also, anchor doesn't work with Pillow 7. I tested with the version 9, 8 and 7.

With the anchor ms (middle baseline) the text is not exactly at the same
position than before. I changed it to md (middle descender) to have the
same position.
See
https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
@brvoisin
Copy link
Contributor Author

brvoisin commented Aug 4, 2022

I fixed the test position by using the anchor md (middle descender) instead of ms (middle baseline). See https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors to understand what it means.

Before:
before
After:
after-fixed

@codecov
Copy link

codecov bot commented Aug 5, 2022

Codecov Report

Merging #165 (e1de637) into main (77c902f) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #165      +/-   ##
==========================================
- Coverage   80.60%   80.58%   -0.03%     
==========================================
  Files          17       17              
  Lines         887      886       -1     
==========================================
- Hits          715      714       -1     
  Misses        172      172              
Impacted Files Coverage Δ
barcode/writer.py 95.04% <100.00%> (-0.03%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@WhyNotHugo
Copy link
Owner

Perfect. I can't even tell the difference in the results (if there is any).

Nice cleanup, thanks for your contribution!

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

Successfully merging this pull request may close these issues.

None yet

2 participants