Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users

Dashboard
Notifications
Mark all as read
Q&A

Convert .npy files in a directory to images (.png)

+1
−4

I have around 20,000 .npy files in a directory. That main directory has no subfolders:-

Main_dir
   |
   |--1.npy
   |--2.npy
   |--3.npy
   |--........

The absolute file paths are stored in a list named paths.

What is the fastest and most efficient method (time and memory-wise) to convert all these .npy files to image (.png preferable)?

I don't care if the solution would use python or bash.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comments

1 answer

+3
−0

A possible parallel solution using imageio:

from imageio import imwrite
from multiprocessing import Pool
from numpy import load
from pathlib import Path


def npy2png(npyFile):
    imwrite(npyFile.stem + '.png', load(npyFile))


rootPath = Path('/path/to/Main_dir')
if __name__ == '__main__':
    with Pool(processes=8) as pool:
        pool.map(npy2png, rootPath.glob('*npy'))

Additional arguments can be passed to imwrite; in the case of the PNG format, execute

import imageio
imageio.help(name='png')

to know which ones are available.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »