2

I have the same image, one of them is the original one stored on my machine, another one uploaded to the azure blob storage and downloaded back to my local machine. Here is a piece of code (Python 3.6.3, Pillow-SIMD 4.3.0post0, libjpeg-turbo, ubuntu 17.10/alpine docker container):

image_stream = open('src/app/resize/resizer/corrupted_blob.jpg', 'rb')
image = Image.open(image_stream)
image.load()

When the interpreter hits the .load method exception is raised: OSError: image file is truncated (6 bytes not processed)

so it happens only with corrupted_blob.jpg, corrupted_original.jpg is loaded and displayed correctly. I know that the first assumption would be 'here is something happened during uploading', but either a browser or windows/ubuntu image viewer or another piece of software which renders images handle both of the images correctly. So there is something with pillow or with underlying libjpeg.

I have tried to use ImageFile.LOAD_TRUNCATED_IMAGES = True but all I get is a black image -_-

Please help!

original image

blob uploaded/downloaded image

A T
  • 81
  • 4

1 Answers1

0

So after investigation, I found out that for some reason suffix JPEG bytes were missing. Here is the workaround:

except OSError as e:
    fixed_image_bytes = image_stream.getvalue() + b'\xff\xd9'
    with io.BytesIO(fixed_image_bytes) as fixed_image_stream:
        image = Image.open(fixed_image_stream)
        image.load()
A T
  • 81
  • 4