Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing PNG files into Numpy?

I have about 200 grayscale PNG images stored within a directory like this.

1.png 2.png 3.png ... ... 200.png 

I want to import all the PNG images as NumPy arrays. How can I do this?

like image 568
pbu Avatar asked Jul 13 '15 14:07

pbu


People also ask

Can you import PNG into Python?

It requires Python 3.5 or any compatible higher version. to access the png module in your Python program. You can also install from source using setuptools . PyPNG uses setup.


Video Answer


2 Answers

According to the doc, scipy.misc.imread is deprecated starting SciPy 1.0.0, and will be removed in 1.2.0. Consider using imageio.imread instead.

Example:

import imageio  im = imageio.imread('my_image.png') print(im.shape) 

You can also use imageio to load from fancy sources:

im = imageio.imread('http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png') 

Edit:

To load all of the *.png files in a specific folder, you could use the glob package:

import imageio import glob  for im_path in glob.glob("path/to/folder/*.png"):      im = imageio.imread(im_path)      print(im.shape)      # do whatever with the image here 
like image 89
Charles Avatar answered Sep 20 '22 08:09

Charles


Using just scipy, glob and having PIL installed (pip install pillow) you can use scipy's imread method:

from scipy import misc import glob  for image_path in glob.glob("/home/adam/*.png"):     image = misc.imread(image_path)     print image.shape     print image.dtype 

UPDATE

According to the doc, scipy.misc.imread is deprecated starting SciPy 1.0.0, and will be removed in 1.2.0. Consider using imageio.imread instead. See the answer by Charles.

like image 37
spoorcc Avatar answered Sep 20 '22 08:09

spoorcc