Skip to content

Commit

Permalink
Merge 4079cd5 into 50c6e35
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Jul 15, 2019
2 parents 50c6e35 + 4079cd5 commit 0a81c28
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
49 changes: 31 additions & 18 deletions elodie/media/photo.py
Expand Up @@ -12,10 +12,8 @@
import re
import time
from datetime import datetime
from PIL import Image
from re import compile


from elodie import log
from .media import Media

Expand All @@ -38,6 +36,15 @@ def __init__(self, source=None):
# We only want to parse EXIF once so we store it here
self.exif = None

# Optionally import Pillow - see gh-325
# https://github.com/jmathai/elodie/issues/325
self.pillow = None
try:
from PIL import Image
self.pillow = Image
except ImportError:
pass

def get_date_taken(self):
"""Get the date which the photo was taken.
Expand Down Expand Up @@ -98,22 +105,28 @@ def is_valid(self):
if(extension != 'heic'):
# gh-4 This checks if the source file is an image.
# It doesn't validate against the list of supported types.
# We check with imghdr and pillow.
if(imghdr.what(source) is None):
# imghdr won't detect all variants of images (https://bugs.python.org/issue28591)
# see https://github.com/jmathai/elodie/issues/281
# before giving up, we use `pillow` imaging library to detect file type
#
# It is important to note that the library doesn't decode or load the
# raster data unless it really has to. When you open a file,
# the file header is read to determine the file format and extract
# things like mode, size, and other properties required to decode the file,
# but the rest of the file is not processed until later.
try:
im = Image.open(source)
except IOError:
# Pillow is used as a fallback and if it's not available we trust
# what imghdr returned.
if(self.pillow is None):
return False

if(im.format is None):
return False

else:
# imghdr won't detect all variants of images (https://bugs.python.org/issue28591)
# see https://github.com/jmathai/elodie/issues/281
# before giving up, we use `pillow` imaging library to detect file type
#
# It is important to note that the library doesn't decode or load the
# raster data unless it really has to. When you open a file,
# the file header is read to determine the file format and extract
# things like mode, size, and other properties required to decode the file,
# but the rest of the file is not processed until later.
try:
im = self.pillow.open(source)
except IOError:
return False

if(im.format is None):
return False

return extension in self.extensions
5 changes: 5 additions & 0 deletions elodie/tests/media/photo_test.py
Expand Up @@ -165,6 +165,11 @@ def test_is_valid_fallback_using_pillow():

assert photo.is_valid()

def test_pillow_not_loaded():
photo = Photo(helper.get_file('imghdr-error.jpg'))
photo.pillow = None

assert photo.is_valid() == False

def test_set_album():
temporary_folder, folder = helper.create_working_folder()
Expand Down

0 comments on commit 0a81c28

Please sign in to comment.