1

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
  • Yes, there are many ways; go ahead and do it; ask a question if you face difficulties Commented Aug 18, 2018 at 11:09

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.