algoo / preview-generator

generates previews of files with cache management
https://pypi.org/project/preview-generator/
MIT License
226 stars 50 forks source link

Preview generator rotating the image by 90 degrees clockwise while generating thumbnail #212

Closed AbhijitKulkarni95 closed 2 years ago

AbhijitKulkarni95 commented 3 years ago

Preview generator rotating the image by 90 degrees clockwise while generating thumbnail

Original image:

original image

Generated thumbnail:

thumb

Please let me know if I have to change something.

Thanks in advance.

inkhey commented 3 years ago

Hello, Thanks for using preview_generator. The issue is related to exif rotation metadata. In fact the original image is in landscape but contain some exif rotation metadata, this way your browser is able to display your image in portrait mode.

In current state preview_generator doesn't look at theses metadata, so the rendered preview image:

So the rendered image by preview_generator is not rotated the way, you expect.

There is already a pending issue about this: https://github.com/algoo/preview-generator/issues/166 the proper way to solve this seems to replace exif rotation metadata in generated preview (to have same behavior).

AbhijitKulkarni95 commented 3 years ago

Hi, Thanks for the reply.

image

By adding image = ImageOps.exif_transpose(image) in image__Pillow.py I was able to resolve this issue.

Any idea if this is the right way?

lebouquetin commented 3 years ago

the proper way to solve this seems to replace exif rotation metadata in generated preview (to have same behavior).

@inkhey way you write "replace exif rotation metadata", you mean "copy rotation metadata from original image to thumbnail one"? If so, it looks like this the right way for me too.

tonypottera24 commented 2 years ago

Any ideas of how to fix this when wand is used?

AbhijitKulkarni95 commented 2 years ago

Any ideas of how to fix this when wand is used?

No idea buddy.

tonypottera24 commented 2 years ago

I followed your suggestions and add these code before I send files into PreviewManager

    try:
        with PILImage.open(image_path) as image:
            image = PILImageOps.exif_transpose(image)
        image.save(image_path)
    except Exception as e:
        print('not a image')
        print(e)
AbhijitKulkarni95 commented 2 years ago

I followed your suggestions and add these code before I send files into PreviewManager

    try:
        with PILImage.open(image_path) as image:
            image = PILImageOps.exif_transpose(image)
        image.save(image_path)
    except Exception as e:
        print('not a image')
        print(e)

You can upgrade Pillow. It is fixed in the latest version. pip install Pillow==8.3.1

inkhey commented 2 years ago

Image rotation has been introduced by https://github.com/algoo/preview-generator/pull/274 About the fact we should probably keep exif instead of rotating image itself, there is a ticket here : https://github.com/algoo/preview-generator/issues/166