0

I have string output from buffer and I want to convert it into the image. I think it's a JPEG2000 format (\x00\x00\x00\x0cjP---). Can you please help me how to do it in Python 3?

My string looks like:

b'\x00\x00\x00\x0cjP \r\n\x87\n\x00\x00\x00\x14ftypjp2 \x00\x00\x00\x00jp2 \x00\x00\x00-jp2h\x00\x00\x00\x16ihdr\x00\x00\x03\xe0\x00\x00\x01@\x00\x01\x07\x07\x00\x00\x00\x00\x00\x0fcolr\x01\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00jp2c\xffO\xffQ\x00)\x00\x00\x00\x00\x01@\x00\x00\x03\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01@\x00\x00\x03\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x07\x01\x01\xffR\x00\x0c\x00\x00\x00\x01\x00\x05\x04\x04\x00\x01\xff\\\x00\x13@@HHPHHPHHPHHPHHP\xff\x90\x00\n\x00\x00\x00\x00\x00\x00\x00\x01\xff\x93\xcf\xb2\xc6\x11PTZ\x10N>\xe9\xe5/|\xeaG\xea\xb5\xd5\xd1\x8d\xd5\xbb\x11\\h\xaf \x13\xd4\x9d\x8c\xb60)\xd9>\xab\xa8\xef\xdcE\x07\xd9\xf5\xcc#\xcc\x8e\x07!------------------------'

martineau
  • 119,623
  • 25
  • 170
  • 301
Param
  • 1
  • I think PIL/Pillow can take a byte string as input. – Mark Ransom Oct 03 '20 at 17:07
  • Thank you Mark for your quick response, I tried PIL and CV2, unfortunately I could nt succeed. Actually I am able to convert the binary string of PNG to image but the same process does not work for JP2 binary string. – Param Oct 03 '20 at 17:13
  • 1
    Param: You must have done something wrong, because the pillow version of the PIL module does indeed support the JP2 format. – martineau Oct 03 '20 at 17:49
  • I explored again the PIL, I found the solution based on your comment, Thank you again. It works like this way: image = BytesIO(jp2_binary_string); final_image= Image.open(image); plt.imshow(final_image); plt.show() – Param Oct 03 '20 at 17:52
  • Thank you Martineau, yes you are right, my mistake was not to convert string to bytes, after using BytesIO(jp2_binary_string) command, it works fine. Thank you again for answering to my question . – Param Oct 03 '20 at 17:59

0 Answers0