python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.32k stars 2.23k forks source link

Problems with image from iPhone 14 #8486

Closed paulocoutinhox closed 1 month ago

paulocoutinhox commented 1 month ago

What did you do?

Im trying make a simple upload from iPhone 14 and save the image.

It works from my iPhone 13 but not for iPhone 14.

I try select different compatible mode, different size etc.

What did you expect to happen?

Normal upload.

What actually happened?

Bad headers errors:

DEBUG 2024-10-19 17:53:50,220 TiffImagePlugin tag: MPFVersion (45056) - type: undefined (7) - value: b'0100'
DEBUG 2024-10-19 17:53:50,220 TiffImagePlugin tag: NumberOfImages (45057) - type: long (4) - value: b'\x00\x00\x00\x02'
DEBUG 2024-10-19 17:53:50,221 TiffImagePlugin tag: MPEntry (45058) - type: undefined (7) Tag Location: 46 - Data Location: 50 - value: b'\x00\x03\x00\x00\x003]\xee\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xe4\xa0\x003$S\x00\x00\x00\x00'
ERROR 2024-10-19 17:53:50,221 validators Failed to validate image: ['Tipo de imagem não suportado para o arquivo: IMG_5615.jpeg. Os tipos permitidos são: jpeg, png, webp.']

What are your OS, Python and Pillow versions?

docker exec -it app-aixdg-1 python3 -m PIL.report
--------------------------------------------------------------------
Pillow 11.0.0
Python 3.10.15 (main, Oct 19 2024, 04:00:28) [GCC 12.2.0]
--------------------------------------------------------------------
Python executable is /usr/local/bin/python3
System Python files loaded from /usr/local
--------------------------------------------------------------------
Python Pillow modules loaded from /app/.local/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /app/.local/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.0.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- JPEG support ok, compiled for libjpeg-turbo 3.0.4
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.8, harfbuzz 10.0.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
import logging

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from PIL import Image

logger = logging.getLogger(__name__)

def validate_image_mimetype(value):
    valid_mime_types = ["jpeg", "png", "webp"]
    allowed_types = ", ".join(valid_mime_types)
    file_name = value.name

    # open the image and check its format
    try:
        image = Image.open(value)
        mime_type = image.format.lower()

        if mime_type not in valid_mime_types:
            raise ValidationError(
                _("error.unsupported-image-type %(file_name)s %(allowed_types)s"),
                params={"file_name": file_name, "allowed_types": allowed_types},
            )
    except Exception as e:
        logger.error(f"Failed to validate image: {e}")

        raise ValidationError(
            _("error.unsupported-image-type %(file_name)s %(allowed_types)s"),
            params={"file_name": file_name, "allowed_types": allowed_types},
        )

If anyone wanna test, it is here: https://aixdg.com/

Thanks.

radarhere commented 1 month ago

Could you post a copy of IMG_5615.jpeg here?

radarhere commented 1 month ago

The DEBUG messages are coming from https://github.com/python-pillow/Pillow/blob/331e4e751733eb0af018602ef26746a7e0571107/src/PIL/TiffImagePlugin.py#L904-L941

I don't think of them as a problem - let us know if you're interested in not showing those.

paulocoutinhox commented 1 month ago

Hi @radarhere, here is the image: https://www.dropbox.com/scl/fi/942uakezlq9amaxqy2g5f/ios-img-error-pillow.jpeg?rlkey=5xncrpabfco7c0xes08rj3cut&dl=1

Error:

DEBUG 2024-10-20 14:29:25,887 TiffImagePlugin tag: MPFVersion (45056) - type: undefined (7) - value: b'0100'
DEBUG 2024-10-20 14:29:25,887 TiffImagePlugin tag: NumberOfImages (45057) - type: long (4) - value: b'\x00\x00\x00\x02'
DEBUG 2024-10-20 14:29:25,887 TiffImagePlugin tag: MPEntry (45058) - type: undefined (7) Tag Location: 46 - Data Location: 50 - value: b'\x00\x03\x00\x00\x00#(\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\xc6\xfc\x00#\x06\xc1\x00\x00\x00\x00'

Scenario: I take a photo direct from iPhone on upload field, nothing more.

radarhere commented 1 month ago

Pillow has identified the image as an MPO.

So I think if you adjust your code to valid_mime_types = ["jpeg", "mpo", "png", "webp"], you will find that the ERROR goes away.

paulocoutinhox commented 1 month ago

Ok, nice. I never will understand this. Thanks.

I have only one problem but i can open another issue if you want.

I want upload a webp file but always get this error:

  File "/opt/homebrew/lib/python3.10/site-packages/django/db/models/fields/files.py", line 338, in pre_save
    file.save(file.name, file.file, save=False)
  File "/opt/homebrew/lib/python3.10/site-packages/django_resized/forms.py", line 136, in save
    thumb.save(new_content, format=img_format, quality=self.field.quality, **img_info)
  File "/opt/homebrew/lib/python3.10/site-packages/PIL/Image.py", line 2605, in save
    save_handler(self, fp, filename)
  File "/opt/homebrew/lib/python3.10/site-packages/PIL/WebPImagePlugin.py", line 300, in _save
    data = _webp.WebPEncode(
ValueError: invalid configuration

Ref: https://github.com/python-pillow/Pillow/blob/ccd4683f452e946887423a746b411c72308e6e39/src/_webp.c#L277

Ref: https://github.com/webmproject/libwebp/blob/dfdcb7f95ca280b2555020115b8f288a5a3453c2/src/enc/config_enc.c#L97

Image: https://www.dropbox.com/scl/fi/0sn27bc1dhf1935nhc1w3/a03cea58-99ea-4219-b603-41e3d9c400be.webp?rlkey=8i0cjw6xs3j208w0qybg8kgpo&dl=1

I tried on linux and macos and already have libwebp installed and pillow return OK for webp module supported.

What can be wrong?

radarhere commented 1 month ago

The following works for me.

from PIL import Image
im = Image.open('in.webp')
im.save('out.webp')

Does it work on your machine? If yes, then something in

thumb.save(new_content, format=img_format, quality=self.field.quality, **img_info)

is triggering the problem. If you can find out what the values of self.field.quality and img_info are, then we can debug this further.

paulocoutinhox commented 1 month ago

Hi, no error happen and the image is saved without problem.

Ok, i will check it. Very thanks for your time.