Skip to content

Commit f6709a9

Browse files
committed
A few minor updates for image exporter
- use `image_type` to avoid shadowing built-in `type` - if condition: pass else: action -> if not condition: action - remove unused vars and imporve style
1 parent 8d0b908 commit f6709a9

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

mfr/extensions/image/export.py

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

1919
def export(self):
2020
parts = self.format.split('.')
21-
type = parts[-1].lower()
22-
max_size = {
23-
'w': None,
24-
'h': None
25-
}
21+
image_type = parts[-1].lower()
22+
max_size = {'w': None, 'h': None}
2623
if len(parts) == 2:
2724
max_size['w'], max_size['h'] = [int(size) for size in parts[0].split('x')]
2825
self.metrics.merge({
29-
'type': type,
26+
'type': image_type,
3027
'max_size_w': max_size['w'],
3128
'max_size_h': max_size['h'],
3229
})
@@ -41,18 +38,14 @@ def export(self):
4138
else:
4239
image = Image.open(self.source_file_path)
4340

44-
if not (max_size.get('w') or max_size.get('h')):
45-
# If any of the dimensions for the resize aren't defined:
46-
pass
47-
else:
41+
# Only resize when both dimensions are available
42+
if max_size.get('w') and max_size.get('h'):
4843
# resize the image to the w/h maximum specified
4944
ratio = min(max_size['w'] / image.size[0], max_size['h'] / image.size[1])
5045
self.metrics.add('ratio', ratio)
5146
if ratio < 1:
52-
image = image.resize(
53-
(round(image.size[0] * ratio), round(image.size[1] * ratio)),
54-
Image.ANTIALIAS
55-
)
47+
size_tuple = (round(image.size[0] * ratio), round(image.size[1] * ratio))
48+
image = image.resize(size_tuple, Image.ANTIALIAS)
5649

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

6457
# handle transparency
6558
# from https://github.com/python-pillow/Pillow/issues/2609
66-
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']:
6760
# JPEG has no transparency, so anything that was transparent gets changed to
6861
# EXPORT_BACKGROUND_COLOR. Default is white.
6962
background = Image.new(image.mode[:-1], image.size, EXPORT_BACKGROUND_COLOR)
7063
background.paste(image, image.split()[-1])
7164
image = background
7265

73-
image.save(self.output_file_path, type)
66+
image.save(self.output_file_path, image_type)
7467
image.close()
7568

7669
except (UnicodeDecodeError, IOError, FileNotFoundError, OSError) as err:
77-
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])
7871
raise exceptions.PillowImageError(
7972
'Unable to export the file as a {}, please check that the '
80-
'file is a valid image.'.format(type),
81-
export_format=type,
73+
'file is a valid image.'.format(image_type),
74+
export_format=image_type,
8275
detected_format=imghdr.what(self.source_file_path),
8376
original_exception=err,
8477
code=400,

0 commit comments

Comments
 (0)