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.

NumPy Array - Importing PNG Files

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.

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.

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 code 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)

Output:

Example: Importing PNG files into NumPy

Python NumPy Programs »



ADVERTISEMENT
ADVERTISEMENT


What's New (MCQs)



Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.