Created
February 7, 2023 11:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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