Pillow ImageQt Module
ImageQt is an important module in the Python image processing library Pillow. It provides the function of converting Pillow image objects into Qt-compatible formats. This module is particularly useful when you need to display images processed by Pillow in Qt applications.
The main role of the ImageQt module is to build a bridge between Pillow and Qt, allowing developers to easily display and manipulate images processed by Pillow in the Qt interface.
The way to import the ImageQt module:
from PIL import ImageQt
Core Method Details
The ImageQt module provides several key methods; detailed descriptions are as follows:
Main methods
| Method | Description | Parameters | Return Value |
|---|---|---|---|
| ImageQt.ImageQt(image) | Convert Pillow image to Qt image object | image: PIL.Image object | QImage or QPixmap object |
| ImageQt.toqimage(image) | Convert Pillow image to QImage object | image: PIL.Image object | QImage object |
| ImageQt.fromqimage(qimage) | Convert QImage object back to Pillow image | qimage: QImage object | PIL.Image object |
Helper methods
| Method | Description | Parameters | Return Value |
|---|---|---|---|
| ImageQt.rgb(r, g, b, a=255) | Create RGBA color value | r,g,b,a: Integer values from 0-255 | 32-bit RGBA value |
| ImageQt.fromqpixmap(qpixmap) | Convert QPixmap object back to Pillow image | qpixmap: QPixmap object | PIL.Image object |
Usage examples
Basic conversion examples
Example
from
PIL
import
Image
from
PIL.
ImageQt
import
ImageQt
from
PyQt5.
QtWidgets
import
QApplication
,
QLabel
Load the image
img
=
Image.
open
(
"example.jpg"
)
Convert to Qt image
qimage
=
ImageQt
(
img
)
Display in Qt
app
=
QApplication
(
[
]
)
label
=
QLabel
(
)
label.
setPixmap
(
qimage
)
label.
show
(
)
app.
exec_
(
)
Bidirectional conversion example
Example
from
PIL
import
Image
from
PIL.
ImageQt
import
fromqimage
,
toqimage
from
PyQt5.
QtGui
import
QImage
Create QImage
qimg
=
QImage
(
100
,
100
,
QImage.
Format_RGB32
)
qimg.
fill
(
Qt.
white
)
Convert to PIL image
pil_img
=
fromqimage
(
qimg
)
Edit image
pil_img
=
pil_img.
rotate
(
45
)
Convert back to QImage
new_qimg
=
toqimage
(
pil_img
)
Notes
-
Qt version compatibility: ImageQt supports PyQt5 and PySide2; ensure the Qt binding you use is consistent with your project.
-
Image format: Not all Pillow image modes support conversion to Qt images. Commonly supported modes include:
-
Performance considerations: Frequently converting between Pillow and Qt image formats affects performance; the number of conversions should be minimized.
-
Memory management: Converted Qt image objects need to be managed by Qt's memory management system; do not delete them manually.
Advanced usage
Handle transparency
Example
Load image with transparency
img
=
Image.
open
(
"transparent.png"
)
.
convert
(
"RGBA"
)
qimg
=
ImageQt
(
img
)
Automatically handle transparency
Custom transformations
Example
Custom color handling
from
PIL
import
ImageOps
img
=
Image.
open
(
"photo.jpg"
)
img
=
ImageOps.
colorize
(
img.
convert
(
"L"
)
,
"black"
,
"gold"
)
qimg
=
ImageQt
(
img
)
By mastering the ImageQt module, you can easily integrate Pillow's powerful image processing functions into Qt applications, providing users with a rich image manipulation experience.