2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# a simple Qt image interface.
|
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 2006-06-03 fl: created
|
|
|
|
# 2006-06-04 fl: inherit from QImage instead of wrapping it
|
|
|
|
# 2006-06-05 fl: removed toimage helper; move string support to ImageQt
|
2013-11-13 15:34:09 +04:00
|
|
|
# 2013-11-13 fl: add support for Qt5 (aurelien.ballier@cyclonit.com)
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Copyright (c) 2006 by Secret Labs AB
|
|
|
|
# Copyright (c) 2006 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2018-05-29 14:21:53 +03:00
|
|
|
import sys
|
2019-07-06 23:40:53 +03:00
|
|
|
from io import BytesIO
|
2024-07-15 12:23:36 +03:00
|
|
|
from typing import TYPE_CHECKING, Callable
|
2019-07-06 23:40:53 +03:00
|
|
|
|
|
|
|
from . import Image
|
2022-04-10 21:20:48 +03:00
|
|
|
from ._util import is_path
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2024-07-15 12:23:36 +03:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from . import ImageFile
|
|
|
|
|
2024-02-10 11:50:45 +03:00
|
|
|
qt_version: str | None
|
2020-12-28 13:58:08 +03:00
|
|
|
qt_versions = [
|
2021-02-10 13:12:30 +03:00
|
|
|
["6", "PyQt6"],
|
2020-12-28 13:58:08 +03:00
|
|
|
["side6", "PySide6"],
|
|
|
|
]
|
2019-02-14 23:59:14 +03:00
|
|
|
|
2018-05-29 14:21:53 +03:00
|
|
|
# If a version has already been imported, attempt it first
|
2024-02-10 11:50:45 +03:00
|
|
|
qt_versions.sort(key=lambda version: version[1] in sys.modules, reverse=True)
|
|
|
|
for version, qt_module in qt_versions:
|
2015-05-07 13:26:26 +03:00
|
|
|
try:
|
2024-02-10 11:50:45 +03:00
|
|
|
QBuffer: type
|
|
|
|
QIODevice: type
|
|
|
|
QImage: type
|
|
|
|
QPixmap: type
|
|
|
|
qRgba: Callable[[int, int, int, int], int]
|
2021-02-10 13:12:30 +03:00
|
|
|
if qt_module == "PyQt6":
|
2021-10-17 05:14:47 +03:00
|
|
|
from PyQt6.QtCore import QBuffer, QIODevice
|
2021-02-10 13:12:30 +03:00
|
|
|
from PyQt6.QtGui import QImage, QPixmap, qRgba
|
|
|
|
elif qt_module == "PySide6":
|
2020-12-28 13:58:08 +03:00
|
|
|
from PySide6.QtCore import QBuffer, QIODevice
|
|
|
|
from PySide6.QtGui import QImage, QPixmap, qRgba
|
2018-05-29 14:21:53 +03:00
|
|
|
except (ImportError, RuntimeError):
|
|
|
|
continue
|
|
|
|
qt_is_installed = True
|
2024-02-10 11:50:45 +03:00
|
|
|
qt_version = version
|
2018-05-29 14:21:53 +03:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
qt_is_installed = False
|
|
|
|
qt_version = None
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2013-11-21 10:20:13 +04:00
|
|
|
def rgb(r, g, b, a=255):
|
2014-09-15 22:24:56 +04:00
|
|
|
"""(Internal) Turns an RGB color into a Qt compatible color integer."""
|
2010-07-31 06:52:47 +04:00
|
|
|
# use qRgb to pack the colors, and then turn the resulting long
|
|
|
|
# into a negative integer with the same bitpattern.
|
2013-11-21 10:20:13 +04:00
|
|
|
return qRgba(r, g, b, a) & 0xFFFFFFFF
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
def fromqimage(im):
|
2016-09-24 12:10:46 +03:00
|
|
|
"""
|
2021-02-16 14:33:17 +03:00
|
|
|
:param im: QImage or PIL ImageQt object
|
2016-09-24 12:10:46 +03:00
|
|
|
"""
|
2014-09-15 22:24:56 +04:00
|
|
|
buffer = QBuffer()
|
2021-10-16 23:04:43 +03:00
|
|
|
if qt_version == "6":
|
|
|
|
try:
|
2021-10-17 05:14:47 +03:00
|
|
|
qt_openmode = QIODevice.OpenModeFlag
|
2021-10-16 23:04:43 +03:00
|
|
|
except AttributeError:
|
|
|
|
qt_openmode = QIODevice.OpenMode
|
|
|
|
else:
|
|
|
|
qt_openmode = QIODevice
|
2021-02-10 13:12:30 +03:00
|
|
|
buffer.open(qt_openmode.ReadWrite)
|
2019-02-14 23:59:14 +03:00
|
|
|
# preserve alpha channel with png
|
2015-09-16 23:59:52 +03:00
|
|
|
# otherwise ppm is more friendly with Image.open
|
|
|
|
if im.hasAlphaChannel():
|
|
|
|
im.save(buffer, "png")
|
|
|
|
else:
|
|
|
|
im.save(buffer, "ppm")
|
2015-06-21 09:31:51 +03:00
|
|
|
|
|
|
|
b = BytesIO()
|
2019-10-07 15:34:12 +03:00
|
|
|
b.write(buffer.data())
|
2014-09-15 22:24:56 +04:00
|
|
|
buffer.close()
|
2015-06-21 09:31:51 +03:00
|
|
|
b.seek(0)
|
|
|
|
|
2015-11-30 14:06:18 +03:00
|
|
|
return Image.open(b)
|
2014-09-15 22:24:56 +04:00
|
|
|
|
|
|
|
|
2024-07-15 12:23:36 +03:00
|
|
|
def fromqpixmap(im) -> ImageFile.ImageFile:
|
2014-09-15 22:24:56 +04:00
|
|
|
return fromqimage(im)
|
|
|
|
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2024-07-15 12:23:36 +03:00
|
|
|
def align8to32(bytes: bytes, width: int, mode: str) -> bytes:
|
2015-09-17 16:32:15 +03:00
|
|
|
"""
|
|
|
|
converts each scanline of data from 8 bit to 32 bit aligned
|
|
|
|
"""
|
|
|
|
|
2021-11-29 06:34:14 +03:00
|
|
|
bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]
|
2015-09-17 16:32:15 +03:00
|
|
|
|
|
|
|
# calculate bytes per line and the extra padding if needed
|
|
|
|
bits_per_line = bits_per_pixel * width
|
|
|
|
full_bytes_per_line, remaining_bits_per_line = divmod(bits_per_line, 8)
|
|
|
|
bytes_per_line = full_bytes_per_line + (1 if remaining_bits_per_line else 0)
|
|
|
|
|
|
|
|
extra_padding = -bytes_per_line % 4
|
|
|
|
|
|
|
|
# already 32 bit aligned by luck
|
|
|
|
if not extra_padding:
|
|
|
|
return bytes
|
|
|
|
|
2023-12-03 22:59:44 +03:00
|
|
|
new_data = [
|
|
|
|
bytes[i * bytes_per_line : (i + 1) * bytes_per_line] + b"\x00" * extra_padding
|
|
|
|
for i in range(len(bytes) // bytes_per_line)
|
|
|
|
]
|
2015-09-17 16:32:15 +03:00
|
|
|
|
2015-09-30 08:28:42 +03:00
|
|
|
return b"".join(new_data)
|
2014-09-15 22:24:56 +04:00
|
|
|
|
2015-12-10 01:35:35 +03:00
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
def _toqclass_helper(im):
|
|
|
|
data = None
|
|
|
|
colortable = None
|
2021-02-11 10:19:05 +03:00
|
|
|
exclusive_fp = False
|
2014-09-15 22:24:56 +04:00
|
|
|
|
|
|
|
# handle filename, if given instead of image name
|
|
|
|
if hasattr(im, "toUtf8"):
|
|
|
|
# FIXME - is this really the best way to do this?
|
2019-09-26 15:12:28 +03:00
|
|
|
im = str(im.toUtf8(), "utf-8")
|
2022-04-10 21:20:48 +03:00
|
|
|
if is_path(im):
|
2015-11-30 14:06:18 +03:00
|
|
|
im = Image.open(im)
|
2021-02-11 10:19:05 +03:00
|
|
|
exclusive_fp = True
|
2014-09-15 22:24:56 +04:00
|
|
|
|
2021-02-10 13:12:30 +03:00
|
|
|
qt_format = QImage.Format if qt_version == "6" else QImage
|
2014-09-15 22:24:56 +04:00
|
|
|
if im.mode == "1":
|
2021-02-10 13:12:30 +03:00
|
|
|
format = qt_format.Format_Mono
|
2014-09-15 22:24:56 +04:00
|
|
|
elif im.mode == "L":
|
2021-02-10 13:12:30 +03:00
|
|
|
format = qt_format.Format_Indexed8
|
2023-12-03 22:59:44 +03:00
|
|
|
colortable = [rgb(i, i, i) for i in range(256)]
|
2014-09-15 22:24:56 +04:00
|
|
|
elif im.mode == "P":
|
2021-02-10 13:12:30 +03:00
|
|
|
format = qt_format.Format_Indexed8
|
2014-09-15 22:24:56 +04:00
|
|
|
palette = im.getpalette()
|
2023-12-03 22:59:44 +03:00
|
|
|
colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)]
|
2014-09-15 22:24:56 +04:00
|
|
|
elif im.mode == "RGB":
|
2021-03-10 05:17:19 +03:00
|
|
|
# Populate the 4th channel with 255
|
|
|
|
im = im.convert("RGBA")
|
|
|
|
|
|
|
|
data = im.tobytes("raw", "BGRA")
|
2021-02-10 13:12:30 +03:00
|
|
|
format = qt_format.Format_RGB32
|
2014-09-15 22:24:56 +04:00
|
|
|
elif im.mode == "RGBA":
|
2020-06-13 15:00:11 +03:00
|
|
|
data = im.tobytes("raw", "BGRA")
|
2021-02-10 13:12:30 +03:00
|
|
|
format = qt_format.Format_ARGB32
|
2024-06-21 17:39:37 +03:00
|
|
|
elif im.mode == "I;16":
|
2021-12-14 02:13:09 +03:00
|
|
|
im = im.point(lambda i: i * 256)
|
|
|
|
|
2021-11-29 06:34:14 +03:00
|
|
|
format = qt_format.Format_Grayscale16
|
2014-09-15 22:24:56 +04:00
|
|
|
else:
|
2021-02-11 10:19:05 +03:00
|
|
|
if exclusive_fp:
|
|
|
|
im.close()
|
2022-12-22 00:51:35 +03:00
|
|
|
msg = f"unsupported image mode {repr(im.mode)}"
|
|
|
|
raise ValueError(msg)
|
2014-09-15 22:24:56 +04:00
|
|
|
|
2021-02-11 10:19:05 +03:00
|
|
|
size = im.size
|
|
|
|
__data = data or align8to32(im.tobytes(), size[0], im.mode)
|
|
|
|
if exclusive_fp:
|
|
|
|
im.close()
|
|
|
|
return {"data": __data, "size": size, "format": format, "colortable": colortable}
|
2014-09-15 22:24:56 +04:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-05-07 13:26:26 +03:00
|
|
|
if qt_is_installed:
|
|
|
|
|
|
|
|
class ImageQt(QImage):
|
2024-07-15 12:23:36 +03:00
|
|
|
def __init__(self, im) -> None:
|
2016-09-24 12:10:46 +03:00
|
|
|
"""
|
|
|
|
An PIL image wrapper for Qt. This is a subclass of PyQt's QImage
|
|
|
|
class.
|
|
|
|
|
2018-10-21 10:26:08 +03:00
|
|
|
:param im: A PIL Image object, or a file name (given either as
|
|
|
|
Python string or a PyQt string object).
|
2016-09-24 12:10:46 +03:00
|
|
|
"""
|
2015-05-07 13:26:26 +03:00
|
|
|
im_data = _toqclass_helper(im)
|
2016-10-27 23:57:11 +03:00
|
|
|
# must keep a reference, or Qt will crash!
|
|
|
|
# All QImage constructors that take data operate on an existing
|
|
|
|
# buffer, so this buffer has to hang on for the life of the image.
|
|
|
|
# Fixes https://github.com/python-pillow/Pillow/issues/1370
|
|
|
|
self.__data = im_data["data"]
|
2016-11-05 20:31:11 +03:00
|
|
|
super().__init__(
|
2016-10-27 23:57:11 +03:00
|
|
|
self.__data,
|
2021-02-11 10:19:05 +03:00
|
|
|
im_data["size"][0],
|
|
|
|
im_data["size"][1],
|
2015-06-19 08:36:23 +03:00
|
|
|
im_data["format"],
|
|
|
|
)
|
2015-05-07 13:26:26 +03:00
|
|
|
if im_data["colortable"]:
|
2015-06-19 08:35:56 +03:00
|
|
|
self.setColorTable(im_data["colortable"])
|
|
|
|
|
|
|
|
|
2024-06-22 03:09:11 +03:00
|
|
|
def toqimage(im) -> ImageQt:
|
2015-06-19 08:35:56 +03:00
|
|
|
return ImageQt(im)
|
|
|
|
|
|
|
|
|
|
|
|
def toqpixmap(im):
|
|
|
|
qimage = toqimage(im)
|
2015-06-19 08:36:23 +03:00
|
|
|
return QPixmap.fromImage(qimage)
|