Make Pillow optional due to external dependencies (#326)
Fixes #325. `Pillow` has a number of external dependencies needed for installation. Since most media files are detected correctly with `imghdr` and `Pillow` is used as a fallback we can make it optional. `Pillow` support was added in #320 to fix #281.
This commit is contained in:
parent
50c6e3597f
commit
87e0420fba
|
@ -12,10 +12,8 @@ import os
|
|||
import re
|
||||
import time
|
||||
from datetime import datetime
|
||||
from PIL import Image
|
||||
from re import compile
|
||||
|
||||
|
||||
from elodie import log
|
||||
from .media import Media
|
||||
|
||||
|
@ -38,6 +36,15 @@ class Photo(Media):
|
|||
# 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.
|
||||
|
||||
|
@ -98,7 +105,13 @@ class Photo(Media):
|
|||
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):
|
||||
# Pillow is used as a fallback and if it's not available we trust
|
||||
# what imghdr returned.
|
||||
if(self.pillow 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
|
||||
|
@ -109,7 +122,7 @@ class Photo(Media):
|
|||
# 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)
|
||||
im = self.pillow.open(source)
|
||||
except IOError:
|
||||
return False
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue