1717import subprocess
1818import sys
1919
20- from PIL import Image , ImageFilter
20+ from PIL import Image , ImageFilter , UnidentifiedImageError
2121from bs4 import BeautifulSoup
2222import six
2323from six .moves .urllib_parse import unquote , urlparse
@@ -46,8 +46,9 @@ class ExifTool(object):
4646 def start_exiftool ():
4747 if shutil .which ("exiftool" ) is None :
4848 log .warning (
49- "EXIF tags will not be copied because the exiftool program could not "
50- "be found. Please install exiftool and make sure it is in your path."
49+ "[image_process] EXIF tags will not be copied because "
50+ "the exiftool program could not be found. "
51+ "Please install exiftool and make sure it is in your path."
5152 )
5253 else :
5354 ExifTool ._instance = ExifTool ()
@@ -110,7 +111,9 @@ def _send_command(self, params):
110111 output += os .read (fd , ExifTool .block_size )
111112 exiftool_result = output .strip ()[: - len (ExifTool .sentinel )]
112113 log .debug (
113- "image_process: exiftool result: {}" .format (exiftool_result .decode ("utf-8" ))
114+ "[image_process] exiftool result: {}" .format (
115+ exiftool_result .decode ("utf-8" )
116+ )
114117 )
115118
116119
@@ -260,7 +263,7 @@ def apply_filter(i, f):
260263
261264
262265def harvest_images (path , context ):
263- log .debug ("process_images: harvest %r" , path )
266+ log .debug ("[image_process] harvesting %r" , path )
264267 # Set default value for 'IMAGE_PROCESS_DIR'.
265268 if "IMAGE_PROCESS_DIR" not in context :
266269 context ["IMAGE_PROCESS_DIR" ] = "derivatives"
@@ -412,6 +415,12 @@ def compute_paths(img, settings, derivative):
412415
413416def process_img_tag (img , settings , derivative ):
414417 path = compute_paths (img , settings , derivative )
418+ if not is_img_identifiable (path .source ):
419+ log .warn (
420+ "[image_process] Skipping image %s that could not be identified by Pillow" ,
421+ path .source ,
422+ )
423+ return
415424 process = settings ["IMAGE_PROCESS" ][derivative ]
416425
417426 img ["src" ] = posixpath .join (path .base_url , path .filename )
@@ -423,16 +432,30 @@ def process_img_tag(img, settings, derivative):
423432 process_image ((path .source , destination , process ), settings )
424433
425434
435+ def is_img_identifiable (img_filepath ):
436+ try :
437+ Image .open (img_filepath )
438+ return True
439+ except (FileNotFoundError , UnidentifiedImageError ):
440+ return False
441+
442+
426443def build_srcset (img , settings , derivative ):
427444 path = compute_paths (img , settings , derivative )
445+ if not is_img_identifiable (path .source ):
446+ log .warn (
447+ "[image_process] Skipping image %s that could not be identified by Pillow" ,
448+ path .source ,
449+ )
450+ return
428451 process = settings ["IMAGE_PROCESS" ][derivative ]
429452
430453 default = process ["default" ]
431454 if isinstance (default , six .string_types ):
432455 breakpoints = {i for i , _ in process ["srcset" ]}
433456 if default not in breakpoints :
434457 log .error (
435- 'image_process: srcset "%s" does not define default "%s"' ,
458+ '[ image_process] srcset "%s" does not define default "%s"' ,
436459 derivative ,
437460 default ,
438461 )
@@ -672,7 +695,7 @@ def process_image(image, settings):
672695 image [1 ] = unquote (image [1 ])
673696 # image[2] is the transformation
674697
675- log .debug ("image_process: {} -> {}" .format (image [0 ], image [1 ]))
698+ log .debug ("[ image_process] {} -> {}" .format (image [0 ], image [1 ]))
676699
677700 os .makedirs (os .path .dirname (image [1 ]), exist_ok = True )
678701
0 commit comments