Now, I had image data(numpy array) after opencv process. I would like insert this image data to excel. But xlswriter only support io.BytesIO stream.
question: I don't know how to convert numpy array to io.BytesIO with jpg format.
I have trying numpy.tostring but without jpg format.
the code working well in below:
_f = open('test.jpg') # I would like to insert test.jpg
worksheet.insert_image('E2', 'abc.jpg', {'image_data': _f.read()})
Anybody can help me ? thank you so much.
Convert the numpy arrays to PIL Image structure, then use BytesIO to store the encoded image.
img_crop_pil = Image.fromarray(numpy_image)
byte_io = BytesIO()
img_crop_pil.save(byte_io, format="JPG")
jpg_buffer = byte_io.getvalue()
byte_io.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With