Pillow ImageQt Module

ImageQt is an important module in the Python image processing library Pillow. It provides functionality to convert Pillow image objects into Qt-compatible formats. This module is especially 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 Qt interfaces.

How to import the ImageQt module:

from PIL import ImageQt

Core Methods Explained

The ImageQt module provides several key methods. Here are their detailed descriptions:

Main Methods

Method Description Parameters Return Value
ImageQt.ImageQt(image) Convert a Pillow image to a Qt image object image: PIL.Image object QImage or QPixmap object
ImageQt.toqimage(image) Convert a Pillow image to a QImage object image: PIL.Image object QImage object
ImageQt.fromqimage(qimage) Convert a QImage object back to a Pillow image qimage: QImage object PIL.Image object

Helper Methods

Method Description Parameters Return Value
ImageQt.rgb(r, g, b, a=255) Create an RGBA color value r,g,b,a: integer values from 0-255 32-bit RGBA value
ImageQt.fromqpixmap(qpixmap) Convert a QPixmap object back to a Pillow image qpixmap: QPixmap object PIL.Image object

Usage Examples

Basic Conversion Example

Example

from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5.QtWidgets import QApplication, QLabel

# Load 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

  1. Qt version compatibility: ImageQt supports PyQt5 and PySide2. Make sure the Qt binding you use matches your project.

  2. Image formats: Not all Pillow image modes support conversion to Qt images. Commonly supported modes include:

    • "L" (8-bit pixels, grayscale)
    • "RGB" (3x8-bit pixels, true color)
    • "RGBA" (4x8-bit pixels, true color with transparency)
  3. Performance considerations: Frequent conversions between Pillow and Qt image formats can affect performance, so you should minimize the number of conversions.

  4. Memory management: Converted Qt image objects need to be managed by Qt's memory management system; do not manually delete them.


Advanced Usage

Handling Transparency

Example

# Load image with transparency
img = Image.open("transparent.png").convert("RGBA")
qimg = ImageQt(img)  # Automatically handle transparency

Custom Conversion

Example

# Custom color processing
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 capabilities into Qt applications, providing users with a rich image manipulation experience.

Other Extensions