Skip to content

Commit d15d322

Browse files
committed
Merge branch 'feautre/fix-image-dimensions' into develop
[SVCS-592] Closes: #340
2 parents 7e1e187 + f6709a9 commit d15d322

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

mfr/extensions/image/export.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ def __init__(self, *args, **kwargs):
1818

1919
def export(self):
2020
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')]
2325
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'],
2729
})
2830
try:
2931
if self.ext in ['.psd']:
@@ -36,13 +38,14 @@ def export(self):
3638
else:
3739
image = Image.open(self.source_file_path)
3840

39-
if max_size:
41+
# Only resize when both dimensions are available
42+
if max_size.get('w') and max_size.get('h'):
4043
# 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])
4245
self.metrics.add('ratio', ratio)
4346
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)
4649

4750
# Mode 'P' is for paletted images. They must be converted to RGB before exporting to
4851
# jpeg, otherwise Pillow will throw an error. This is a temporary workaround, as the
@@ -53,22 +56,22 @@ def export(self):
5356

5457
# handle transparency
5558
# 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']:
5760
# JPEG has no transparency, so anything that was transparent gets changed to
5861
# EXPORT_BACKGROUND_COLOR. Default is white.
5962
background = Image.new(image.mode[:-1], image.size, EXPORT_BACKGROUND_COLOR)
6063
background.paste(image, image.split()[-1])
6164
image = background
6265

63-
image.save(self.output_file_path, type)
66+
image.save(self.output_file_path, image_type)
6467
image.close()
6568

6669
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])
6871
raise exceptions.PillowImageError(
6972
'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,
7275
detected_format=imghdr.what(self.source_file_path),
7376
original_exception=err,
7477
code=400,

0 commit comments

Comments
 (0)