Search code examples
pythonnumpymemorypython-imaging-library

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:

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)