Kozea / cairocffi

CFFI-based cairo bindings for Python.
https://doc.courtbouillon.org/cairocffi
BSD 3-Clause "New" or "Revised" License
208 stars 54 forks source link

`cairocffi.ImageSurface.create_from_png` fails for non-PNG formats with `cairo 1.18.2` #229

Closed elParaguayo closed 2 months ago

elParaguayo commented 3 months ago

Arch Linux has just packaged cairo 1.18.2. Unfortunately, this has broken the bar in qtile for several users.

From what I can tell, the issue is that cairocffi.ImageSurface.create_from_png no longer works with other image formats. With cairo 1.18.0 we could load things like SVG images with that method but in 1.18.2 we end up with the following error:

  File "/usr/lib/python3.12/site-packages/libqtile/images.py", line 56, in get_cairo_surface
    surf = cairocffi.ImageSurface.create_from_png(io.BytesIO(bytes_img))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/cairocffi/surfaces.py", line 785, in create_from_png
    Surface.__init__(self, pointer)  # Skip ImageSurface.__init__
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/cairocffi/surfaces.py", line 158, in __init__
    self._check_status()
  File "/usr/lib/python3.12/site-packages/cairocffi/surfaces.py", line 170, in _check_status
    _check_status(cairo.cairo_surface_status(self._pointer))
  File "/usr/lib/python3.12/site-packages/cairocffi/__init__.py", line 87, in _check_status
    raise exception(message, status)
cairocffi.CairoError: cairo returned CAIRO_STATUS_PNG_ERROR: b'error occurred in libpng while reading from or writing to a PNG file

I appreciate that the method name is create_from_png but it's worked with other formats before. Is this a bug in cairo, does cairocffi need updating or should we be using a different method to load images into an ImageSurface?

Arch has only packaged python-cairocffi 1.6.1 - I haven't tested the code with 1.7.1. (EDIT: now tested - same error).

Thanks in advance for any help you can provide here.

elParaguayo commented 2 months ago

Closing this. We can just use cairocffi.pixbuf.decode_to_image_surface.