Importing PNG files into Numpy?

后端 未结 7 1399
轮回少年
轮回少年 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 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']
    
    [255 255 255]
    

提交回复
热议问题