Skip to content

Commit

Permalink
RichText: use custom QImage to PIL conversion if built-in conversion …
Browse files Browse the repository at this point in the history
…fails

- Fixes Pillow >= 10 and PyQt6 combination
  • Loading branch information
smathot committed Dec 22, 2023
1 parent 5514425 commit 31df5ed
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions openexp/_canvas/_richtext/richtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
QStyleOptionGraphicsItem,
QApplication
)
from qtpy.QtGui import QPixmap, QPainter, QColor, QFont, QFontDatabase
from qtpy.QtGui import QPixmap, QPainter, QColor, QFont, QFontDatabase, QImage
from qtpy.QtCore import Qt, QCoreApplication
from PIL import Image

Expand Down Expand Up @@ -212,10 +212,35 @@ def _to_qimage(self):
t.paint(painter, QStyleOptionGraphicsItem(), None)
painter.end()
return pixmap.toImage()

def _qimage_to_pil(self, qimage):
"""This functions takes a QImage and returns a PIL Image. If available,
this uses PIL's built-in conversion tool, but since this has broken
for PyQt5 for Pillow >= 10, we resort to a custom conversion procedure
if necesary.

Check failure on line 220 in openexp/_canvas/_richtext/richtext.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

necesary ==> necessary
"""
try:
return Image.fromqimage(self._to_qimage())
except ImportError:
pass
import numpy as np
oslogger.info('no Qt bindings, using custom QImage to PIL conversion')
qimage = qimage.convertToFormat(QImage.Format_ARGB32)
width = qimage.width()
height = qimage.height()
ptr = qimage.bits()
ptr.setsize(qimage.byteCount())
# Assuming 32-bit color depth
arr = np.array(ptr).reshape(height, width, 4)
if arr.shape[2] == 4:
arr = arr[..., [2, 1, 0, 3]] # Convert ARGB to RGBA
else:
arr = arr[..., [2, 1, 0]] # Convert RGB to BGR
return Image.fromarray(arr)

def _to_pil(self):

im = Image.fromqimage(self._to_qimage())
im = self._qimage_to_pil(self._to_qimage())
bbox = im.getbbox()
x1, y1, x2, y2 = (0, 0, 1, 1) if bbox is None else bbox
y1 = min(y2 - self.font_size, y1)
Expand Down

0 comments on commit 31df5ed

Please sign in to comment.