AppDividend
Latest Code Tutorials

How to Convert PIL Image to Numpy Array in Python

0

To convert the PIL Image to Numpy array, use the np.array() method and pass the image data to the np.array() method. It will return the array consists of pixel values. Pillow is the Python imaging library that supports a range of image file formats such as PNG, JPEG, PPM, GIF, TIFF, and BMP.

Pillow supports operations like cropping, resizing, adding text to images, rotating, greyscaling.

Convert PIL Image to Numpy Array

To convert the PIL Image to Numpy array, first, we have to open the Image using PIL’s Image module. The Image module provides the Image.open() method. Then we get the image data and then pass the image data to the np.array() method to get the array of image data.

Our original image is the following.

edit image using numpy array

Now, let’s convert this Image into a numpy array.

import numpy as np
from PIL import Image

img_data = Image.open('forest.jpg')
img_arr = np.array(img_data)
print(img_arr)

Output

[[[178 204 231]
  [173 199 226]
  [174 200 227]
  ...
  [153 188 218]
  [153 188 218]
  [154 189 219]]

 [[174 200 227]
  [171 197 224]
  [175 201 228]
  ...
  [151 186 216]
  [149 184 214]
  [147 182 212]]

 [[171 197 224]
  [170 196 223]
  [175 201 228]
  ...
  [150 185 215]
  [147 182 212]
  [144 179 209]]

 ...

 [[130  94  46]
  [142 105  53]
  [182 143  86]
  ...
  [ 56  55  51]
  [ 52  50  53]
  [ 44  41  48]]

 [[137  96  66]
  [126  83  48]
  [154 111  66]
  ...
  [ 43  42  37]
  [ 38  36  39]
  [ 37  34  43]]

 [[ 98  56  32]
  [110  67  35]
  [130  87  44]
  ...
  [ 40  39  34]
  [ 37  35  40]
  [ 44  41  52]]]

You can see in the output that we get the numpy array of image data.

We have used the Image.open() method and np.array() method to convert PIL Image into Numpy array.

The shape of the img_arr is the following.

import numpy as np
from PIL import Image

img_data = Image.open('forest.jpg')
img_arr = np.array(img_data)
print(img_arr.shape)

Output

(6000, 4000, 3)

Convert Numpy Array to PIL Image

To convert a Numpy Array to PIL Image, we can use the Image.fromarray() method.

If we want to change, modify or edit the Image using numpy, then first, we convert into numpy array and then perform the mathematical operation to edit the array and then convert back into the Image using Image.array() method.

We can even modify the img_arr by subtracting the values and then create an image from the array using fromarray() function and save the image into the file system.

import numpy as np
from PIL import Image

img_data = Image.open('forest.jpg')
img_arr = np.array(img_data)
print(img_arr.shape)

img_arr = img_arr - 180
new_img = Image.fromarray(img_arr)
new_img.save("altered_forest.png")

Output

 

Convert PIL Image to Numpy Array in Python

In this example, we have converted a PIL Image to Numpy array using the np array() method and then modify its pixel and converted the array to the PIL image using the fromarray() method.

Conclusion

In machine learning, Python uses the image data in the form of Numpy array, i.e., [Height, Width, Channel] format. To enhance the performance of the predictive model, we have to know how to load and manipulate images.

In this example, we have seen how to load an image, convert the Image into a numpy array, modify the numpy array, and then convert it back to image.

That is it for this tutorial. Thanks for taking it.

See also

How to read an image using OpenCV

How to convert RGB Image to Grayscale

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.