WhyNotHugo / python-barcode

㊙️ Create standard barcodes with Python. No external dependencies. 100% Organic Python.
http://python-barcode.rtfd.io/
MIT License
558 stars 123 forks source link

Unable to generate PNG using pillow 10 #205

Closed pokoli closed 1 year ago

pokoli commented 1 year ago

Since the release of Pillow 10.0.0 the followin traceback is shown when generating a png barcode:

Traceback (most recent call last):
  File "/home/pokoli/projectes/tryton/test_barcode.py", line 30, in <module>
    image = generate_png("8749903790831")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pokoli/projectes/tryton/test_barcode.py", line 26, in generate_png
    Generator(code, writer=writer).write(output, options=options)
  File "/home/pokoli/.virtualenvs/tryton/lib/python3.11/site-packages/barcode/base.py", line 82, in write
    output = self.render(options, text)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pokoli/.virtualenvs/tryton/lib/python3.11/site-packages/barcode/ean.py", line 126, in render
    return super().render(options, text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pokoli/.virtualenvs/tryton/lib/python3.11/site-packages/barcode/base.py", line 105, in render
    raw = self.writer.render(code)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pokoli/.virtualenvs/tryton/lib/python3.11/site-packages/barcode/writer.py", line 265, in render
    self._callbacks["paint_text"](xpos, ypos)
  File "/home/pokoli/.virtualenvs/tryton/lib/python3.11/site-packages/barcode/writer.py", line 441, in _paint_text
    width, height = font.getsize(subtext)
                    ^^^^^^^^^^^^
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'

Here is a sample script to reproduce the issue:

from io import BytesIO

import barcode
from barcode.writer import ImageWriter

def generate_png(
        code, width=2, height=150, border=6.5,
        font_size=10, text_distance=5.0,
        background='white', foreground='black'):
    dpi = 300
    width = width * 25.4 / dpi
    height = height * 25.4 / dpi
    writer = ImageWriter()
    options = dict(
        format='png',
        module_with=width,
        module_height=height,
        quiet_zone=border,
        font_size=font_size,
        text_distance=text_distance,
        background=background,
        foreground=foreground)
    output = BytesIO()
    Generator = barcode.get('ean')
    Generator(code, writer=writer).write(output, options=options)
    return output

image = generate_png("8749903790831")

Same code works correctly with pillow < 10

pokoli commented 1 year ago

I see that this has been fixed with 53b52351d4c50a873e0dd5c1c33e7bae16c1b128 I tested the same script on main branch of the repository and it works correctly.

It will be great if you can publish a release to make this fix available.

WhyNotHugo commented 1 year ago

New release is out

WhyNotHugo commented 1 year ago

https://python-barcode.readthedocs.io/en/stable/changelog.html