Importing PNG files into Numpy?

后端 未结 7 1378
轮回少年
轮回少年 2020-12-08 18:32

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 i

相关标签:
7条回答
  • 2020-12-08 18:47

    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
    
    0 讨论(0)
  • 2020-12-08 18:49

    Using a (very) commonly used package is prefered:

    import matplotlib.pyplot as plt
    im = plt.imread('image.png')
    
    0 讨论(0)
  • 2020-12-08 18:51

    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.

    0 讨论(0)
  • 2020-12-08 18:52

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

    from PIL import Image
    import numpy as np
    
    im_frame = Image.open(path_to_file + 'file.png')
    np_frame = np.array(im_frame.getdata())
    

    Note: The .getdata() might not be needed - np.array(im_frame) should also work

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-08 19:00

    I like the build-in pathlib libary because of quick options like directory= Path.cwd() Together with opencv it's quite easy to read pngs to numpy arrays. In this example you can even check the prefix of the image.

    from pathlib import Path
    import cv2
    prefix = "p00"
    suffix = ".png"
    directory= Path.cwd()
    file_names= [subp.name for subp in directory.rglob('*') if  (prefix in subp.name) & (suffix == subp.suffix)]
    file_names.sort()
    print(file_names)
    
    all_frames= []
    for file_name in file_names:
        file_path = str(directory / file_name)
        frame=cv2.imread(file_path)
        all_frames.append(frame)
    print(type(all_frames[0]))
    print(all_frames[0] [1][1])
    

    Output:

    ['p000.png', 'p001.png', 'p002.png', 'p003.png', 'p004.png', 'p005.png', 'p006.png', 'p007.png', 'p008.png', 'p009.png']
    <class 'numpy.ndarray'>
    [255 255 255]
    
    0 讨论(0)
提交回复
热议问题