-1

So I have this websocket call, where I hand over a file from a html form to my python backend. Everything is working fine I would say except that my file itself will not save back to a image.

I will retrieve the image in this format:

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00c\x00\x00\x00c\x01\x03\x00\x00\x00\xb5\xf5,\xd6\x00\x00\x00\x06PLTE\x00\x00\x00\xff\xff\xff\xa5\xd9\x9f\xdd\x00\x00\x00\x02tRNS\xff\xff\xc8\xb5\xdf\xc7\x00\x00\x00\tpHYs\x00\x00\x0b\x12\x00\x00\x0b\x12\x01\xd2\xdd~\xfc\x00\x00\x00\xf1IDAT8\x8d\xcd\xd41\x8e\xc4 \x0c\x05PG\x14\x94s\x01$\xae\x91.W\x82\x0b\x84\xe1\x02\x93+\xd1q\x8dH\\ \xee(P<\x1e\xcd\xeef\x1b\xe2h\x8b\xd5\xb8\xe2u\xfe6\x00\xf4\xbb\xe0\x83\xb5\x01\xcc\x93\t\t\x06IH-$\xb5d>HJ\xc6is\xcb\xe0/h\x86\x82\xd7\x14\xf2z\xbb"j\xf3D\xf1\xe8\xac+\xce\xe7\xb3\xf1G\xda\xae\xb86\xad\xe21\xc1\xae6\rCj\xa0[\x90\x84\xb9\xc4DwX\xbd\xa4\x1d\xb8k\x9eDAID\n+-\xd5FQ\x95\x93\xd9\x07\xd0"i\x9f\xcacTD_\xbd\x9c\x08+\xcc\xa3q\xd0\x06I\xbb.<\x8c\xc0)%aZal\x0e\x14I\xe2\xc2\xd4<YQ\xbc[7\x95\x98\x7f6\xdd\x15\xdf\x177R$\x8b\xa2\x92\xf1\xd9npI\xa1\xf2*\xbe3\x9c\xca\xd7\xd7\x1d\xf4\xa2\x88\xc3\xf1c}o\xecL\xaf|\xbc\x84z\xa4\xed\xe9\xbf\x7f\xa2\xbf\xe9\t\xb2\xe0\xbc\x1a\xa1l|\xbb\x00\x00\x00\x00IEND\xaeB`\x82'

I understand this is a png file in plain text format where all the \x.. are non printable characters. What I want to do is convert that back into a file object which should hold things like mimetype, filename and finally the image itself.

How can I do that?

Holger
  • 285,553
  • 42
  • 434
  • 765
Patrick Hener
  • 135
  • 5
  • 16
  • 1
    Does this answer your question? [PIL: Convert Bytearray to Image](https://stackoverflow.com/questions/18491416/pil-convert-bytearray-to-image) – Arkistarvh Kltzuonstev May 06 '20 at 08:14
  • That text format, actually contains the mimetype. So you probably want to reshape your question to better match what it is you want to achieve. Voting to close either because it's lacking details in what you want to achieve or because it's solved by the above comment. – Torxed May 06 '20 at 08:16

1 Answers1

0

This is a binary format just write it in a file and you will find your image. By the way it's a QR Code!

data=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00c\x00\x00\x00c\x01\x03\x00\x00\x00\xb5\xf5,\xd6\x00\x00\x00\x06PLTE\x00\x00\x00\xff\xff\xff\xa5\xd9\x9f\xdd\x00\x00\x00\x02tRNS\xff\xff\xc8\xb5\xdf\xc7\x00\x00\x00\tpHYs\x00\x00\x0b\x12\x00\x00\x0b\x12\x01\xd2\xdd~\xfc\x00\x00\x00\xf1IDAT8\x8d\xcd\xd41\x8e\xc4 \x0c\x05PG\x14\x94s\x01$\xae\x91.W\x82\x0b\x84\xe1\x02\x93+\xd1q\x8dH\\ \xee(P<\x1e\xcd\xeef\x1b\xe2h\x8b\xd5\xb8\xe2u\xfe6\x00\xf4\xbb\xe0\x83\xb5\x01\xcc\x93\t\t\x06IH-$\xb5d>HJ\xc6is\xcb\xe0/h\x86\x82\xd7\x14\xf2z\xbb"j\xf3D\xf1\xe8\xac+\xce\xe7\xb3\xf1G\xda\xae\xb86\xad\xe21\xc1\xae6\rCj\xa0[\x90\x84\xb9\xc4DwX\xbd\xa4\x1d\xb8k\x9eDAID\n+-\xd5FQ\x95\x93\xd9\x07\xd0"i\x9f\xcacTD_\xbd\x9c\x08+\xcc\xa3q\xd0\x06I\xbb.<\x8c\xc0)%aZal\x0e\x14I\xe2\xc2\xd4<YQ\xbc[7\x95\x98\x7f6\xdd\x15\xdf\x177R$\x8b\xa2\x92\xf1\xd9npI\xa1\xf2*\xbe3\x9c\xca\xd7\xd7\x1d\xf4\xa2\x88\xc3\xf1c}o\xecL\xaf|\xbc\x84z\xa4\xed\xe9\xbf\x7f\xa2\xbf\xe9\t\xb2\xe0\xbc\x1a\xa1l|\xbb\x00\x00\x00\x00IEND\xaeB`\x82'
newFile = open("image.png", "wb")
newFile.write(data)
newFile.close()

Then just load it.