Posted in

How to convert a numpy array to an image with Pillow?

Hey there! I’m a supplier of Pillow, the super – cool Python library for image processing. Today, I’m gonna walk you through how to convert a NumPy array to an image using Pillow. Pillow

First off, let’s understand what we’re dealing with. NumPy is an awesome Python library for numerical operations. It’s great for handling multi – dimensional arrays and matrices. On the other hand, Pillow is a powerful library for working with images in Python. It can open, manipulate, and save many different image file formats.

So, why would you want to convert a NumPy array to an image? Well, there are tons of reasons. Maybe you’ve been doing some data analysis or machine learning, and you’ve generated a NumPy array that represents an image. Or perhaps you’re working on a computer vision project, and you need to visualize the results in an image format.

Prerequisites

Before we start, make sure you have both NumPy and Pillow installed. You can install them using pip. Just open your terminal and run:

pip install numpy pillow

The Basics of NumPy Arrays and Pillow Images

A NumPy array is a grid of values, all of the same type. For an image, this grid usually represents the pixels. Each element in the array corresponds to a pixel, and the value of that element represents the color of the pixel.

Pillow, on the other hand, uses the Image class to represent images. An Image object has properties like width, height, and mode (e.g., ‘RGB’ for a color image).

Converting a NumPy Array to a Pillow Image

Let’s start with a simple example. Suppose you have a NumPy array that represents a grayscale image. Here’s how you can convert it to a Pillow image:

import numpy as np
from PIL import Image

# Create a simple NumPy array representing a grayscale image
# Let's make a 100x100 array filled with random values between 0 and 255
image_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)

# Convert the NumPy array to a Pillow image
image = Image.fromarray(image_array)

# Save the image
image.save('random_grayscale_image.png')

In this code, we first import the necessary libraries: numpy and PIL.Image. Then we create a 100×100 NumPy array filled with random integers between 0 and 255. The dtype=np.uint8 is important because Pillow expects the pixel values to be in the range of 0 – 255, and uint8 (unsigned 8 – bit integer) is the appropriate data type for this.

Next, we use the Image.fromarray() method to convert the NumPy array to a Pillow Image object. Finally, we save the image as a PNG file.

Handling Color Images

If you’re working with color images, things are a bit more complicated. A color image in RGB format has three channels: red, green, and blue. So, your NumPy array should have three dimensions: height, width, and 3 (for the three color channels).

Here’s an example:

import numpy as np
from PIL import Image

# Create a 100x100 color image array
image_array = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)

# Convert the NumPy array to a Pillow image
image = Image.fromarray(image_array)

# Save the image
image.save('random_color_image.png')

In this case, we create a 100x100x3 NumPy array, where the third dimension represents the RGB channels. Then we convert it to a Pillow image and save it.

Dealing with Different Data Types

Sometimes, your NumPy array might have a different data type than uint8. For example, you might have floating – point values. In that case, you need to convert the data type to uint8 before converting to a Pillow image.

Here’s how you can do it:

import numpy as np
from PIL import Image

# Create a NumPy array with floating - point values
image_array = np.random.rand(100, 100)

# Scale the values to the range 0 - 255 and convert to uint8
scaled_array = (image_array * 255).astype(np.uint8)

# Convert the NumPy array to a Pillow image
image = Image.fromarray(scaled_array)

# Save the image
image.save('scaled_image.png')

In this code, we first create a NumPy array with floating – point values between 0 and 1. Then we scale these values to the range 0 – 255 and convert the data type to uint8. Finally, we convert the array to a Pillow image and save it.

Advanced Techniques

If you want to do more complex operations, like resizing the image or changing its color mode, Pillow has a lot of built – in methods.

For example, let’s say you want to resize the image after converting it from a NumPy array:

import numpy as np
from PIL import Image

# Create a NumPy array
image_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)

# Convert the NumPy array to a Pillow image
image = Image.fromarray(image_array)

# Resize the image
resized_image = image.resize((200, 200))

# Save the resized image
resized_image.save('resized_image.png')

In this code, we first convert the NumPy array to a Pillow image. Then we use the resize() method to change the size of the image to 200×200 pixels. Finally, we save the resized image.

Conclusion

Converting a NumPy array to an image with Pillow is a pretty straightforward process. Whether you’re working with grayscale or color images, Pillow provides a simple and efficient way to do it.

Table Cloth As a Pillow supplier, I can offer you high – quality support and resources for all your image – processing needs. If you’re looking to integrate Pillow into your projects, whether it’s for data visualization, computer vision, or any other application, I’d love to have a chat with you. Reach out to me to discuss your requirements and let’s work together to make your projects a success!

References

  • NumPy Documentation
  • Pillow Documentation

Jiangsu Sidefu Textile Co., Ltd.
Jiangsu Sidefu Textile Co., Ltd. is well-known as one of the professional manufacturers and suppliers of various linen products in China, is equipped with hundreds of professional staff and advanced equipment. We have served many five-star hotels and owned good reputation for the quality of our products. Now, welcome to buy or wholesale pillow with our factory.
Address: No.101 Yongxing Road, Chongchuan Zone, Nantong, Jiangsu, China
E-mail: info@sidefu-china.com
WebSite: https://www.sidefuchina.com/