Importing PNG files into NumPy

Learn, how to import PNG files into NumPy in Python?
Submitted by Pranit Sharma, on January 18, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

The PNG file format is widely used on websites to display high-quality digital images. Created to exceed the performance of GIF files, PNGs offer not just lossless compression, but also a much broader and brighter color palette.

Problem statement

Suppose we are given a png image file and we need to import this png file using NumPy. As we know a picture contains pixels arranged as a matrix hence we can import the pixels of this image as a matrix.

NumPy Array - Importing PNG Files

For this purpose, we need to import the imageio library which has imread() method with the help of which we can load a png file.

Let us understand with the help of an example,

Python program to import PNG files into NumPy

# Import imageio
import imageio

# Import numpy
import numpy

# Loading the image
img = imageio.imread('google.png')

# Print the shape of the image
print("Shape of image:\n",img.shape)


The output of the above program is:

Example: Importing PNG files into NumPy

Python NumPy Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.