Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to load images from memory to numpy using file system

I want to store my image directory in memory, then load the images into a numpy array.

The normative way to load images that are not in memory is as follows:

import PIL.Image
import numpy as np

image = PIL.Image.open("./image_dir/my_image_1.jpg")
image = np.array(image)

However, I am not sure how to do this when the images are in memory. So far, I have been able to setup the following starter code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import fs
import fs.memoryfs
import fs.osfs

image_dir = "./image_dir"

mem_fs = fs.memoryfs.MemoryFS()
drv_fs = fs.osfs.OSFS(image_path)

fs.copy.copy_fs(drv_fs, mem_fs)

print(mem_fs.listdir('.'))

Returns:

['my_image_1.jpg', 'my_image_2.jpg']

How do I load images that are in memory into numpy?

I am also open to alternatives to the fs package.

>Solution :

As per the documentation, Pillow’s Image.open accepts a file object instead of a file name, so as long as your in-memory file package provides Python file objects (which it most likely does), you can just use them. If it doesn’t, you could even just wrap them in a class that provides the required methods. Assuming you are using PyFilesystem, according to its documentation you should be fine.

So, you want something like:

import numpy as np
import PIL.Image
import fs.memoryfs
import fs.osfs
import fs.copy

mem_fs = fs.memoryfs.MemoryFS()
drv_fs = fs.osfs.OSFS("./image_dir")

fs.copy.copy_file(drv_fs, './my_image_1.jpg', mem_fs, 'test.jpg')

with mem_fs.openbin('test.jpg') as f:
    image = PIL.Image.open(f)
    image = np.array(image)

(note I just used copy_file because I tested with a single file, you can use copy_fs if you need to copy the entire tree – it’s the same principle)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading