PIL Image to Numpy Array

P

PIL Image to Numpy Array

When working with images in Python, the Python Imaging Library (PIL) is a popular choice for image manipulation. Numpy is a powerful library for numerical operations in Python, and being able to convert PIL images to Numpy arrays can be very useful.

In this article, we will explore how to convert a PIL image to a Numpy array, and provide code examples to demonstrate the process.

Converting a PIL Image to Numpy Array

To convert a PIL image to a Numpy array, we can use the numpy.array() function. This function takes as input a PIL image object and returns a Numpy array representing the image.

Here is an example code snippet showing how to convert a PIL image to a Numpy array:

from PIL import Image
import numpy as np

# Load a sample image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array)

Load a sample image:

PIL Image to Numpy Array

Output:

PIL Image to Numpy Array

In this example, we first load a sample image using the Image.open() function from PIL. We then convert the image to a Numpy array using the np.array() function and store the result in image_array. Finally, we print the Numpy array representing the image.

Code Examples of convert PIL images to Numpy arrays

Here are 10 code examples demonstrating how to convert a PIL image to a Numpy array.

Note: The images we use are all paths on the D:/web_numpywhere/output/img/pil_image_to_numpy.jpg.

PIL Image to Numpy Array

Example 1

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.shape)

Output:

PIL Image to Numpy Array

Example 2

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.dtype)

Output:

PIL Image to Numpy Array

Example 3

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.max())

Output:

PIL Image to Numpy Array

Example 4

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.min())

Output:

PIL Image to Numpy Array

Example 5

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.mean())

Output:

PIL Image to Numpy Array

Example 6

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.shape)

Output:

PIL Image to Numpy Array

Example 7

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.size)

Output:

PIL Image to Numpy Array

Example 8

from PIL import Image
import numpy as np

# Load image
image = Image.open("D:/web_numpywhere/output/img/pil_image_to_numpy.jpg")

# Convert image to Numpy array
image_array = np.array(image)

print(image_array.dtype)

Output:

PIL Image to Numpy Array

Conclusion of convert PIL images to Numpy arrays

In this article, we have discussed how to convert a PIL image to a Numpy array in Python. By using the numpy.array() function, we can easily convert PIL images to Numpy arrays and perform various operations on them. This can be useful for tasks such as image processing, machine learning, and computer vision.

If you are working with images in Python, understanding how to convert between PIL images and Numpy arrays is an essential skill to have. With the code examples provided in this article, you should now have a good understanding of how to perform this conversion and work with images in Python using Numpy.

Latest Articles

Popular Articles