Importing PNG files into Numpy?


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 into Numpy and then later want to apply k-means to generate a dictionary of patches using k-means (scikit)



Does anybody know a python library that could load these images into numpy on a fly?




5 Answers
5



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


pip install pillow


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.


scipy.misc.imread


imageio.imread instead





Just change to glob.glob("./train/*.png")
– pbu
Jul 13 '15 at 14:53


glob.glob("./train/*.png")





scipy.misc.imread is deprecated. See my answer below!
– Charles
Oct 31 '17 at 20:48


scipy.misc.imread



Bit late to the party, but the current answer is now deprecated.



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.


scipy.misc.imread


imageio.imread



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





Downvoter, if you could please help me make this answer better by telling me what to improve, it would be very appreciated!
– Charles
May 22 at 21:55



This can also be done with the Image class of the PIL library:


Image


from PIL import Image
import numpy as np

im_frame = Image.open(path_to_file + 'file.png')
np_frame = np.array(im_frame.getdata())



I changed a bit and it worked like this, dumped into one single array, provided all the images are of same dimensions.


png =
for image_path in glob.glob("./train/*.png"):
png.append(misc.imread(image_path))

im = np.asarray(png)

print 'Importing done...', im.shape





Perfect. Great All-In-One Solution. I had stored images into an np.array, but then ran into trouble as the array had (shape == (num_images,) with each image (shape == (32,32,3)). Your solution (plus im = np.reshape(num_images,32,32,3) works brilliantly! :-)
– SherylHohman
Mar 29 '17 at 2:15



im = np.reshape(num_images,32,32,3)





typo: I don't even need the reshape call above. In mine vexed hack, massaging it into that desired shape was getting messy. Thanks for the direct path.
– SherylHohman
Mar 29 '17 at 2:24




If you are loading images, you are likely going to be working with one or both of matplotlib and opencv to manipulate and view the images.


matplotlib


opencv



For this reason, I tend to use their image readers and append those to lists, from which I make a NumPy array.


import os
import matplotlib.pyplot as plt
import cv2
import numpy as np

# Get the file paths
im_files = os.listdir('path/to/files/')

# imagine we only want to load PNG files (or JPEG or whatever...)
EXTENSION = '.png'

# Load using matplotlib
images_plt = [plt.imread(f) for f in im_files if f.endswith(EXTENSION)]
# convert your lists into a numpy array of size (N, H, W, C)
images = np.array(images_plt)

# Load using opencv
images_cv = [cv2.imread(f) for f in im_files if f.endswith(EXTENSION)]
# convert your lists into a numpy array of size (N, C, H, W)
images = np.array(images_cv)



The only difference to be aware of is the following:



So a single image that is 256*256 in size would produce matrices of size (3, 256, 256) with opencv and (256, 256, 3) using matplotlib.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Boo (programming language)

How to make file upload 'Required' in Contact Form 7?