Pillow is a Python imaging library that is used to handle image processing tasks in Python. It is a fork of the Python Imaging Library (PIL), which was discontinued in 2011. Pillow is compatible with both Python 2 and 3, and it is used to open, manipulate, and save different image file formats.

In this article, we will discuss how to read and process images using Pillow in Python. We will explore various methods provided by Pillow to read images, such as fromarray, frombuffer, frombytes, fromqimage, and fromqpixmap.

Reading Images using Pillow

The first step in image processing is to read the image. Pillow provides different methods to read the image depending on the format and the source of the image. Here are some of the methods used to read images using Pillow.

fromarray() method

This method is used to create an image object from a NumPy array. Here is an example:

import numpy as np
from PIL import Image

# create a NumPy array of size 100x100 and fill it with zeros
img_array = np.zeros((100,100), dtype=np.uint8)

# create an image object from the array
img = Image.fromarray(img_array)

# show the image
img.show()

In this example, we created a NumPy array of size 100x100 and filled it with zeros. Then we used the fromarray() method to create an image object from the array. Finally, we displayed the image using the show() method.

frombuffer() method

This method is used to create an image object from a buffer object. Here is an example:

from PIL import Image

# create a buffer object
buffer = bytes([255, 0, 0, 0, 255, 0, 0, 0, 255])

# create an image object from the buffer
img = Image.frombuffer('RGB', (3, 1), buffer)

# show the image
img.show()

In this example, we created a buffer object with the RGB values of three pixels. Then we used the frombuffer() method to create an image object from the buffer. Finally, we displayed the image using the show() method.

frombytes() method

This method is used to create an image object from a bytes object. Here is an example:

from PIL import Image

# create a bytes object
bytes_data = bytes([255, 255, 255, 0, 0, 0, 255, 0, 0, 0, 0, 0, 255, 0, 0, 0])

# create an image object from the bytes
img = Image.frombytes('RGBA', (2, 2), bytes_data)

# show the image
img.show()

In this example, we created a bytes object with the RGBA values of four pixels. Then we used the frombytes() method to create an image object from the bytes. Finally, we displayed the image using the show() method.

fromqimage() method

This method is used to create an image object from a QImage object. Here is an example:

from PIL.ImageQt import ImageQt
from PyQt5.QtGui import QImage, QColor

# create a QImage object
qimg = QImage(100, 100, QImage.Format_RGB32)

# fill the image with red color
red = QColor(255, 0, 0)
for x in range(100):
    for y in range(100):
        qimg.setPixelColor(x, y, red)

# create an ImageQt object from the QImage
imageqt = ImageQt(qimg)

# create an image object from the ImageQt object
img = Image.fromqimage(imageqt)

# show the image
img.show()

In this example, we created a QImage object of size 100x100 with the RGB32 format. Then we filled the image with red color using the setPixelColor() method. Next, we created an ImageQt object from the QImage and used it to create an image object using the fromqimage() method. Finally, we displayed the image using the show() method.

In this article, we discussed how to read and process images using Pillow in Python. We explored various methods provided by Pillow to read images, such as 'fromarray', 'frombuffer', 'frombytes' and 'fromqimage'. These methods provide different ways to create an image object from various sources such as NumPy arrays, buffer objects, bytes objects and QImage objects objects. With these methods, we can easily read and process different types of images in our Python applications.