Skip to content

Detect mimetypes#3525

Merged
hugovk merged 3 commits intopython-pillow:masterfrom
radarhere:mimetype
Jan 5, 2019
Merged

Detect mimetypes#3525
hugovk merged 3 commits intopython-pillow:masterfrom
radarhere:mimetype

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Jan 2, 2019

#3501 (comment) reports that one of Django's tests is currently failing, as a call to Pillow from Django - Image.MIME.get(image.format) - is reporting that PNG has mimetype 'image/apng'.

A simple way to replicate this in just Pillow is

>>> from PIL import Image
>>> Image.preinit()
>>> Image.MIME.get('PNG')
'image/apng'

This is because

Image.register_mime(PngImageFile.format, "image/png")
Image.register_mime(PngImageFile.format, "image/apng")

calls register_mime twice

Pillow/src/PIL/Image.py

Lines 2806 to 2814 in e32e3fd

def register_mime(id, mimetype):
"""
Registers an image MIME type. This function should not be used
in application code.
:param id: An image format identifier.
:param mimetype: The image MIME type for this format.
"""
MIME[id.upper()] = mimetype

So the dictionary keeps the information from the last call.

However, this problem does not just exist for PNG. It also exists for Jpeg2K and SGI. This PR solves this by changing to just one register_mimetype call and overriding that default by setting ImageFile.custom_mimetype when appropriate, to be returned by the ImageFile.get_format_mimetype() method.

This PR will fix the Django test as it currently exists.

On SGI systems, SGI image files end with the extension .bw if they are B/W images, they end in .rgb if they contain RGB image data, and end in .rgba if they are RGB images with alpha channel.

Sometimes the .sgi extension is used as well.

Presuming that the mime type should follow suit, and given that indeterminate description, my suggestion is that 'image/rgb' be used for RGB image data, and 'image/sgi' be used otherwise.

The File Type box BR field is "jpx " (note the trailing SPACE character)

The additional test image is taken from https://raw.githubusercontent.com/openpreserve/format-corpus/master/jp2k-formats/balloon.jpf, which is CC0 according to https://github.com/openpreserve/format-corpus

@hugovk hugovk mentioned this pull request Jan 2, 2019
20 tasks
nijel added a commit to WeblateOrg/weblate that referenced this pull request Jan 2, 2019
Not sure if supporting apng is reasonable, but PNG is currently detectes
as apng by Pillow, see python-pillow/Pillow#3525

Signed-off-by: Michal Čihař <michal@cihar.com>
@hugovk hugovk merged commit 33d3beb into python-pillow:master Jan 5, 2019
@radarhere radarhere deleted the mimetype branch January 5, 2019 19:15
Millak pushed a commit to Millak/guix that referenced this pull request Jan 13, 2019
I'm looking at this, as this release fixes an issue with the Django testsuite:

python-pillow/Pillow#3525

* gnu/packages/python.scm (python-pillow): Update to 5.4.1.
radarhere added a commit to radarhere/Pillow that referenced this pull request Mar 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants