I have a device that accepts bitmap binary data. I would like to convert a numpy 2d array to bitmap and send it to this device. Currently what I do is to save the 2d array to a bitmap file, then read it into a variable and send that to the device. I'd like to skip the writing to the disk step. Is there an easy way to do that in Python?
1 Answer
You can use io.BytesIO as a memory buffer to store the bitmap and send it without writing to disk.
As an example, assuming you use PIL or Pillow to save your bitmap file :
import io
from PIL import Image
image = Image.fromarray(numpy_array)
if image.mode != 'RGB':
image = image.convert('RGB')
with io.BytesIO() as f:
image.save(f, format='BMP')
send_to_device(f)