Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Created February 7, 2023 11:48
Show Gist options
  • Save ei-grad/a84c1acab659f180c720483a9bb884c9 to your computer and use it in GitHub Desktop.
Save ei-grad/a84c1acab659f180c720483a9bb884c9 to your computer and use it in GitHub Desktop.
Example of a shared buffer between file (mmap), numpy array and Pillow image
import mmap
from PIL import Image
import numpy as np
size = 32 * 32 * 4
f = open('f.bin', 'w+b')
f.truncate(size)
mm = mmap.mmap(f.fileno(), size)
mm[:] = (np.arange(size) % 199).astype(np.uint8)
data = np.frombuffer(mm, dtype=np.uint8).reshape(32, 32, 4)
img = Image.fromarray(data, 'RGBX')
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment