Skip to content

Commit

Permalink
Merged in #138 (mime type introspection)
Browse files Browse the repository at this point in the history
  • Loading branch information
respondcreate committed Oct 30, 2019
2 parents 40a5f82 + db8837c commit f9e46fb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 45 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"creating new images from the one assigned to the field.",
long_description=open('README.rst').read(),
zip_safe=False,
install_requires=['Pillow>=2.4.0'],
install_requires=['Pillow>=2.4.0', 'python-magic>=0.4.15,<1.0.0'],
include_package_data=True,
keywords=[
'django',
Expand Down
1 change: 1 addition & 0 deletions test_reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ coverage==3.7.1
coveralls==0.5
djangorestframework==3.10.3
flake8==2.2.5
python-magic==0.4.15
requests==2.5.1
sphinx-rtd-theme==0.1.6
4 changes: 2 additions & 2 deletions versatileimagefield/datastructures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.files.uploadedfile import InMemoryUploadedFile

from ..settings import QUAL, VERSATILEIMAGEFIELD_PROGRESSIVE_JPEG
from ..utils import get_image_metadata_from_file_ext
from ..utils import get_image_metadata_from_file

EXIF_ORIENTATION_KEY = 274

Expand Down Expand Up @@ -138,8 +138,8 @@ def preprocess_JPEG(self, image, **kwargs):
def retrieve_image(self, path_to_image):
"""Return a PIL Image instance stored at `path_to_image`."""
image = self.storage.open(path_to_image, 'rb')
image_format, mime_type = get_image_metadata_from_file(image)
file_ext = path_to_image.rsplit('.')[-1]
image_format, mime_type = get_image_metadata_from_file_ext(file_ext)

return (
Image.open(image),
Expand Down
65 changes: 23 additions & 42 deletions versatileimagefield/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os

import magic
from django.core.exceptions import ImproperlyConfigured

from .settings import (
Expand All @@ -16,45 +17,22 @@

# PIL-supported file formats as found here:
# https://infohost.nmt.edu/tcc/help/pubs/pil/formats.html
# (PIL Identifier, mime type)
BMP = ('BMP', 'image/bmp')
DCX = ('DCX', 'image/dcx')
EPS = ('eps', 'image/eps')
GIF = ('GIF', 'image/gif')
JPEG = ('JPEG', 'image/jpeg')
PCD = ('PCD', 'image/pcd')
PCX = ('PCX', 'image/pcx')
PDF = ('PDF', 'application/pdf')
PNG = ('PNG', 'image/png')
PPM = ('PPM', 'image/x-ppm')
PSD = ('PSD', 'image/psd')
TIFF = ('TIFF', 'image/tiff')
XBM = ('XBM', 'image/x-xbitmap')
XPM = ('XPM', 'image/x-xpm')

# Mapping file extensions to PIL types/mime types
FILE_EXTENSION_MAP = {
'png': PNG,
'jpe': JPEG,
'jpeg': JPEG,
'jpg': JPEG,
'gif': GIF,
'bmp': BMP,
'dib': BMP,
'dcx': DCX,
'eps': EPS,
'ps': EPS,
'pcd': PCD,
'pcx': PCX,
'pdf': PDF,
'pbm': PPM,
'pgm': PPM,
'ppm': PPM,
'psd': PSD,
'tif': TIFF,
'tiff': TIFF,
'xbm': XBM,
'xpm': XPM
# {mime type: PIL Identifier}
MIME_TYPE_TO_PIL_IDENTIFIER = {
'image/bmp': 'BMP',
'image/dcx': 'DCX',
'image/eps': 'eps',
'image/gif': 'GIF',
'image/jpeg': 'JPEG',
'image/pcd': 'PCD',
'image/pcx': 'PCX',
'application/pdf': 'PDF',
'image/png': 'PNG',
'image/x-ppm': 'PPM',
'image/psd': 'PSD',
'image/tiff': 'TIFF',
'image/x-xbitmap': 'XBM',
'image/x-xpm': 'XPM',
}


Expand Down Expand Up @@ -163,14 +141,17 @@ def get_filtered_path(path_to_image, filename_key, storage):
return path_to_return


def get_image_metadata_from_file_ext(file_ext):
def get_image_metadata_from_file(file_like):
"""
Receive a valid image file format and returns a 2-tuple of two strings:
Receive a valid image file and returns a 2-tuple of two strings:
[0]: Image format (i.e. 'jpg', 'gif' or 'png')
[1]: InMemoryUploadedFile-friendly save format (i.e. 'image/jpeg')
image_format, in_memory_file_type
"""
return FILE_EXTENSION_MAP.get(file_ext, JPEG)
mime_type = magic.from_buffer(file_like.read(1024), mime=True)
file_like.seek(0)
image_format = MIME_TYPE_TO_PIL_IDENTIFIER[mime_type]
return image_format, mime_type


def validate_versatileimagefield_sizekey_list(sizes):
Expand Down

0 comments on commit f9e46fb

Please sign in to comment.