@@ -18,12 +18,14 @@ def __init__(self, *args, **kwargs):
18
18
19
19
def export (self ):
20
20
parts = self .format .split ('.' )
21
- type = parts [- 1 ].lower ()
22
- max_size = [int (x ) for x in parts [0 ].split ('x' )] if len (parts ) == 2 else None
21
+ image_type = parts [- 1 ].lower ()
22
+ max_size = {'w' : None , 'h' : None }
23
+ if len (parts ) == 2 :
24
+ max_size ['w' ], max_size ['h' ] = [int (size ) for size in parts [0 ].split ('x' )]
23
25
self .metrics .merge ({
24
- 'type' : type ,
25
- 'max_size_w' : max_size [0 ],
26
- 'max_size_h' : max_size [1 ],
26
+ 'type' : image_type ,
27
+ 'max_size_w' : max_size ['w' ],
28
+ 'max_size_h' : max_size ['h' ],
27
29
})
28
30
try :
29
31
if self .ext in ['.psd' ]:
@@ -36,13 +38,14 @@ def export(self):
36
38
else :
37
39
image = Image .open (self .source_file_path )
38
40
39
- if max_size :
41
+ # Only resize when both dimensions are available
42
+ if max_size .get ('w' ) and max_size .get ('h' ):
40
43
# resize the image to the w/h maximum specified
41
- ratio = min (max_size [0 ] / image .size [0 ], max_size [1 ] / image .size [1 ])
44
+ ratio = min (max_size ['w' ] / image .size [0 ], max_size ['h' ] / image .size [1 ])
42
45
self .metrics .add ('ratio' , ratio )
43
46
if ratio < 1 :
44
- image = image . resize (( round (image .size [0 ] * ratio ),
45
- round ( image .size [ 1 ] * ratio )) , Image .ANTIALIAS )
47
+ size_tuple = ( round (image .size [0 ] * ratio ), round ( image . size [ 1 ] * ratio ))
48
+ image = image .resize ( size_tuple , Image .ANTIALIAS )
46
49
47
50
# Mode 'P' is for paletted images. They must be converted to RGB before exporting to
48
51
# jpeg, otherwise Pillow will throw an error. This is a temporary workaround, as the
@@ -53,22 +56,22 @@ def export(self):
53
56
54
57
# handle transparency
55
58
# from https://github.com/python-pillow/Pillow/issues/2609
56
- if image .mode in ('RGBA' , 'RGBa' , 'LA' ) and type in ['jpeg' , 'jpg' ]:
59
+ if image .mode in ('RGBA' , 'RGBa' , 'LA' ) and image_type in ['jpeg' , 'jpg' ]:
57
60
# JPEG has no transparency, so anything that was transparent gets changed to
58
61
# EXPORT_BACKGROUND_COLOR. Default is white.
59
62
background = Image .new (image .mode [:- 1 ], image .size , EXPORT_BACKGROUND_COLOR )
60
63
background .paste (image , image .split ()[- 1 ])
61
64
image = background
62
65
63
- image .save (self .output_file_path , type )
66
+ image .save (self .output_file_path , image_type )
64
67
image .close ()
65
68
66
69
except (UnicodeDecodeError , IOError , FileNotFoundError , OSError ) as err :
67
- name , extension = os .path .splitext (os .path .split (self .source_file_path )[- 1 ])
70
+ os .path .splitext (os .path .split (self .source_file_path )[- 1 ])
68
71
raise exceptions .PillowImageError (
69
72
'Unable to export the file as a {}, please check that the '
70
- 'file is a valid image.' .format (type ),
71
- export_format = type ,
73
+ 'file is a valid image.' .format (image_type ),
74
+ export_format = image_type ,
72
75
detected_format = imghdr .what (self .source_file_path ),
73
76
original_exception = err ,
74
77
code = 400 ,
0 commit comments