0

I'm having a Unity function writen in C# to allow users to upload customized images. The image is sent as byte[] in string format. I have a simple flask backend server to save the images. However, when I worked with io.BytesIO and Image.open() functions, it shows cannot identify image file <_io.BytesIO object at 0x7f5599f43308> Here is my code:

@app.route("/post_file",methods=['GET','POST'])
def post_file():
    data = request.form.to_dict()
    json_byte = data['jsonbytes'] # posting a json byte[] and image byte[] with multi-form
    image_byte = data['imagebytes'] # a byte[] in string type
    image_byte = data['imagebytes'].encode() # byte[] type
    imageBytesIO=io.BytesIO(image_byte)
    imageBytesIO.seek(0)
    image = Image.open(imageBytesIO).convert('RGBA')

The code breaks at the last line.

 File "/home/iedl/editor/editor_app/app.py", line 37, in post_file
  image = Image.open(imageBytesIO).convert('RGBA')
  File "/home/iedl/editor/editor_env/lib/python3.5/site-packages/PIL/Image.py", line 2818, in open
    raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x7f5599f43308>

qqxlafk
  • 129
  • 1
  • 1
  • 6
  • Please show the first 20 bytes of `image_byte`. – Mark Setchell Nov 18 '20 at 08:22
  • Does this answer your question? [PIL cannot identify image file for io.BytesIO object](https://stackoverflow.com/questions/31077366/pil-cannot-identify-image-file-for-io-bytesio-object) – Adam Strauss Nov 18 '20 at 08:40
  • @MarkSetchell it prints out messy code... `�PNG���IHDR����` something like this – qqxlafk Nov 18 '20 at 08:42
  • I don't use JSON much, but I think I thought that it maybe probably possibly can't store null bytes... which may occur in PNG/JPEG or other binary image formats. Most people seem to base64 encode images in JSON. I may be completely wrong here... someone can kindly correct me if so. It will make it a bit slower and 30% bigger, mind you. – Mark Setchell Nov 18 '20 at 09:05

0 Answers0