1

I am trying to convert, what I think is a bytearray pulled from a database to an image using python PIL.

I have access to the imagetype(jpg, png,..), the image height/width, and the bytearray. The bytearray is of the format

0xffd8ffe000104a46494600010101006000600000ffe111164578.....

I have tried many of the PIL options such as .fromstring and .frombuffer. I've also tried converting the bytearray into other forms (Base64,etc..) and then converting it to an image from there. None of these have worked and the image file is always corrupted.

If i follow the advice from this question:

image = Image.open(io.BytesIO(imagestring))
image.save(imageToSave.jpg)

I get this error: IOError: cannot identify image file

type(imagestring) returns type 'bytearray'

Thanks for reading and for any answers, let me know if I should edit this post with more info.

T rumsey
  • 45
  • 1
  • 10
  • There is no need to make this assumption, what does `type(imagestring)` give you? I'm gonna bet it's a `bytes` object. But how, exactly, are you pulling this from a data-base? – juanpa.arrivillaga Nov 09 '17 at 00:13
  • It returns . I'm pulling it from an mssql database with a python connection. – T rumsey Nov 09 '17 at 00:18
  • So, it sounds like whatever you are pulling from the data-base is not something Pillow can handle. Perhaps it's base64 encoded? Hard to say. But I've used that construction many times – juanpa.arrivillaga Nov 09 '17 at 00:25

1 Answers1

0

I can't explain why the 0x is there and if it will be a problem other people run into, but I had to trim off the 0x from the start of the string:

0xffd8ffe000104a46494600010101006000600000ffe111164578....

The code:

image = Image.open(io.BytesIO(imagestring))
image.save(imageToSave.jpg)

worked fine after that

T rumsey
  • 45
  • 1
  • 10