ImageQt.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # a simple Qt image interface.
  6. #
  7. # history:
  8. # 2006-06-03 fl: created
  9. # 2006-06-04 fl: inherit from QImage instead of wrapping it
  10. # 2006-06-05 fl: removed toimage helper; move string support to ImageQt
  11. # 2013-11-13 fl: add support for Qt5 (aurelien.ballier@cyclonit.com)
  12. #
  13. # Copyright (c) 2006 by Secret Labs AB
  14. # Copyright (c) 2006 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. from __future__ import annotations
  19. import sys
  20. from io import BytesIO
  21. from typing import TYPE_CHECKING, Any, Callable, Union
  22. from . import Image
  23. from ._util import is_path
  24. if TYPE_CHECKING:
  25. import PyQt6
  26. import PySide6
  27. from . import ImageFile
  28. QBuffer: type
  29. QByteArray = Union[PyQt6.QtCore.QByteArray, PySide6.QtCore.QByteArray]
  30. QIODevice = Union[PyQt6.QtCore.QIODevice, PySide6.QtCore.QIODevice]
  31. QImage = Union[PyQt6.QtGui.QImage, PySide6.QtGui.QImage]
  32. QPixmap = Union[PyQt6.QtGui.QPixmap, PySide6.QtGui.QPixmap]
  33. qt_version: str | None
  34. qt_versions = [
  35. ["6", "PyQt6"],
  36. ["side6", "PySide6"],
  37. ]
  38. # If a version has already been imported, attempt it first
  39. qt_versions.sort(key=lambda version: version[1] in sys.modules, reverse=True)
  40. for version, qt_module in qt_versions:
  41. try:
  42. qRgba: Callable[[int, int, int, int], int]
  43. if qt_module == "PyQt6":
  44. from PyQt6.QtCore import QBuffer, QIODevice
  45. from PyQt6.QtGui import QImage, QPixmap, qRgba
  46. elif qt_module == "PySide6":
  47. from PySide6.QtCore import QBuffer, QIODevice
  48. from PySide6.QtGui import QImage, QPixmap, qRgba
  49. except (ImportError, RuntimeError):
  50. continue
  51. qt_is_installed = True
  52. qt_version = version
  53. break
  54. else:
  55. qt_is_installed = False
  56. qt_version = None
  57. def rgb(r: int, g: int, b: int, a: int = 255) -> int:
  58. """(Internal) Turns an RGB color into a Qt compatible color integer."""
  59. # use qRgb to pack the colors, and then turn the resulting long
  60. # into a negative integer with the same bitpattern.
  61. return qRgba(r, g, b, a) & 0xFFFFFFFF
  62. def fromqimage(im: QImage | QPixmap) -> ImageFile.ImageFile:
  63. """
  64. :param im: QImage or PIL ImageQt object
  65. """
  66. buffer = QBuffer()
  67. qt_openmode: object
  68. if qt_version == "6":
  69. try:
  70. qt_openmode = getattr(QIODevice, "OpenModeFlag")
  71. except AttributeError:
  72. qt_openmode = getattr(QIODevice, "OpenMode")
  73. else:
  74. qt_openmode = QIODevice
  75. buffer.open(getattr(qt_openmode, "ReadWrite"))
  76. # preserve alpha channel with png
  77. # otherwise ppm is more friendly with Image.open
  78. if im.hasAlphaChannel():
  79. im.save(buffer, "png")
  80. else:
  81. im.save(buffer, "ppm")
  82. b = BytesIO()
  83. b.write(buffer.data())
  84. buffer.close()
  85. b.seek(0)
  86. return Image.open(b)
  87. def fromqpixmap(im: QPixmap) -> ImageFile.ImageFile:
  88. return fromqimage(im)
  89. def align8to32(bytes: bytes, width: int, mode: str) -> bytes:
  90. """
  91. converts each scanline of data from 8 bit to 32 bit aligned
  92. """
  93. bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]
  94. # calculate bytes per line and the extra padding if needed
  95. bits_per_line = bits_per_pixel * width
  96. full_bytes_per_line, remaining_bits_per_line = divmod(bits_per_line, 8)
  97. bytes_per_line = full_bytes_per_line + (1 if remaining_bits_per_line else 0)
  98. extra_padding = -bytes_per_line % 4
  99. # already 32 bit aligned by luck
  100. if not extra_padding:
  101. return bytes
  102. new_data = [
  103. bytes[i * bytes_per_line : (i + 1) * bytes_per_line] + b"\x00" * extra_padding
  104. for i in range(len(bytes) // bytes_per_line)
  105. ]
  106. return b"".join(new_data)
  107. def _toqclass_helper(im: Image.Image | str | QByteArray) -> dict[str, Any]:
  108. data = None
  109. colortable = None
  110. exclusive_fp = False
  111. # handle filename, if given instead of image name
  112. if hasattr(im, "toUtf8"):
  113. # FIXME - is this really the best way to do this?
  114. im = str(im.toUtf8(), "utf-8")
  115. if is_path(im):
  116. im = Image.open(im)
  117. exclusive_fp = True
  118. assert isinstance(im, Image.Image)
  119. qt_format = getattr(QImage, "Format") if qt_version == "6" else QImage
  120. if im.mode == "1":
  121. format = getattr(qt_format, "Format_Mono")
  122. elif im.mode == "L":
  123. format = getattr(qt_format, "Format_Indexed8")
  124. colortable = [rgb(i, i, i) for i in range(256)]
  125. elif im.mode == "P":
  126. format = getattr(qt_format, "Format_Indexed8")
  127. palette = im.getpalette()
  128. assert palette is not None
  129. colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)]
  130. elif im.mode == "RGB":
  131. # Populate the 4th channel with 255
  132. im = im.convert("RGBA")
  133. data = im.tobytes("raw", "BGRA")
  134. format = getattr(qt_format, "Format_RGB32")
  135. elif im.mode == "RGBA":
  136. data = im.tobytes("raw", "BGRA")
  137. format = getattr(qt_format, "Format_ARGB32")
  138. elif im.mode == "I;16":
  139. im = im.point(lambda i: i * 256)
  140. format = getattr(qt_format, "Format_Grayscale16")
  141. else:
  142. if exclusive_fp:
  143. im.close()
  144. msg = f"unsupported image mode {repr(im.mode)}"
  145. raise ValueError(msg)
  146. size = im.size
  147. __data = data or align8to32(im.tobytes(), size[0], im.mode)
  148. if exclusive_fp:
  149. im.close()
  150. return {"data": __data, "size": size, "format": format, "colortable": colortable}
  151. if qt_is_installed:
  152. class ImageQt(QImage): # type: ignore[misc]
  153. def __init__(self, im: Image.Image | str | QByteArray) -> None:
  154. """
  155. An PIL image wrapper for Qt. This is a subclass of PyQt's QImage
  156. class.
  157. :param im: A PIL Image object, or a file name (given either as
  158. Python string or a PyQt string object).
  159. """
  160. im_data = _toqclass_helper(im)
  161. # must keep a reference, or Qt will crash!
  162. # All QImage constructors that take data operate on an existing
  163. # buffer, so this buffer has to hang on for the life of the image.
  164. # Fixes https://github.com/python-pillow/Pillow/issues/1370
  165. self.__data = im_data["data"]
  166. super().__init__(
  167. self.__data,
  168. im_data["size"][0],
  169. im_data["size"][1],
  170. im_data["format"],
  171. )
  172. if im_data["colortable"]:
  173. self.setColorTable(im_data["colortable"])
  174. def toqimage(im: Image.Image | str | QByteArray) -> ImageQt:
  175. return ImageQt(im)
  176. def toqpixmap(im: Image.Image | str | QByteArray) -> QPixmap:
  177. qimage = toqimage(im)
  178. pixmap = getattr(QPixmap, "fromImage")(qimage)
  179. if qt_version == "6":
  180. pixmap.detach()
  181. return pixmap