Search code examples
pythonimagenumpypng

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?


Solution

  • 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.