GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python NumPy: Convert a PIL Image into a NumPy array - w3resource

NumPy: Convert a PIL Image into a NumPy array

Last update on August 29 2025 12:45:27 (UTC/GMT +8 hours)


Convert PIL Image to NumPy Array

Write a NumPy program to convert a PIL image into a NumPy array.

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Importing the PIL library for handling image data
import PIL

# Opening an image file 'w3resource-logo.png' using PIL's Image module and storing the image data in 'img_data'
img_data = PIL.Image.open('w3resource-logo.png')

# Converting the image data into a NumPy array and storing it in 'img_arr'
img_arr = np.array(img_data)

# Printing the NumPy array representation of the image
print(img_arr) 

Sample Output:

[[[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

 ...

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]]

Explanation:

In the above code -

img_data = PIL.Image.open('w3resource-logo.png'): Read the image file 'w3resource-logo.png' using PIL's Image.open() function and store the image data in the variable 'img_data'.

img_arr = np.array(img_data): Convert the image data stored in 'img_data' to a NumPy array named 'img_arr'. The NumPy array will have a shape corresponding to the image's height, width, and the number of color channels (typically 3 for RGB images or 4 for RGBA images).

Finally print() function prints the NumPy array 'img_arr' to the console.


For more Practice: Solve these Related Problems:

  • Write a program to load a PIL image and convert it into a NumPy array, then verify the array’s shape.
  • Create a function that transforms a PIL image into grayscale and returns the corresponding NumPy array.
  • Test the conversion with images of different modes (RGB, L, RGBA) and compare the array dimensions.
  • Implement error handling to manage cases when the input is not a valid image.

Go to:

  • NumPy Array Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Calculate Percentiles of Array
NEXT : Convert NumPy Array to Image


Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.