11import os
2- import imghdr
32import warnings
43
54from PIL import Image
@@ -14,7 +13,7 @@ class ImageExporter(extension.BaseExporter):
1413
1514 def __init__ (self , * args , ** kwargs ):
1615 super ().__init__ (* args , ** kwargs )
17- self .metrics .add ('pil_version' , Image .VERSION )
16+ self .metrics .add ('pil_version' , Image .__version__ )
1817
1918 def export (self ):
2019 parts = self .format .split ('.' )
@@ -45,7 +44,7 @@ def export(self):
4544 self .metrics .add ('ratio' , ratio )
4645 if ratio < 1 :
4746 size_tuple = (round (image .size [0 ] * ratio ), round (image .size [1 ] * ratio ))
48- image = image .resize (size_tuple , Image .ANTIALIAS )
47+ image = image .resize (size_tuple , Image .Resampling . LANCZOS )
4948
5049 # Mode 'P' is for paletted images. They must be converted to RGB before exporting to
5150 # jpeg, otherwise Pillow will throw an error. This is a temporary workaround, as the
@@ -72,7 +71,14 @@ def export(self):
7271 'Unable to export the file as a {}, please check that the '
7372 'file is a valid image.' .format (image_type ),
7473 export_format = image_type ,
75- detected_format = imghdr . what ( self .source_file_path ),
74+ detected_format = self .detect_image_format ( ),
7675 original_exception = err ,
7776 code = 400 ,
7877 )
78+
79+ def detect_image_format (self ):
80+ try :
81+ with Image .open (self .source_file_path ) as img :
82+ return img .format .lower ()
83+ except Exception :
84+ return None
0 commit comments