Skip to content

Image from /view endpoint has wrong orientation #1621

@RaySuhyunLee

Description

@RaySuhyunLee

What's the problem?

/view endpoint returns image with wrong orientation when:

  1. The image has EXIF orientation AND
  2. preview parameter is passed OR channel parameter is not "rgba"

Untitled-2

Why does it happen?

# server.py / view_image()
# modified a little bit for better understanding
...
if 'preview' in request.rel_url.query:
    with Image.open(file) as img:
        ... # do some image processing
        return web.Response(body=buffer.read(), content_type='image/png',
                            headers={"Content-Disposition": f"filename=\"{filename}\""})

elif channel == 'rgb':
    with Image.open(file) as img:
        ... # do some image processing
        return web.Response(body=buffer.read(), content_type='image/png',
                            headers={"Content-Disposition": f"filename=\"{filename}\""})

elif channel == 'a':
    with Image.open(file) as img:
        ... # same here
        return web.Response(body=alpha_buffer.read(), content_type='image/png',
                            headers={"Content-Disposition": f"filename=\"{filename}\""})
else:
    return web.FileResponse(file, headers={"Content-Disposition": f"filename=\"{filename}\""})

As you see, view_image() returns the original file with EXIF info in else condtion, and therefore the orientation in this case is correct.
However, in other cases, it opens the image, performs some image processing, and then returns the modified image without EXIF information. Therefore the resulting image will have wrong orientation if the image has EXIF orientation not equal to 1.
(Refer to this for more information about EXIF orientation)

How can we fix it?

Adding the below one line will solve the problem:

# right after opening the image
img = ImageOps.exif_transpose(img)  # this corrects the image orientation according to the EXIF info

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions