Skip to content

Commit

Permalink
Merge cb07abd into 62e0cde
Browse files Browse the repository at this point in the history
  • Loading branch information
NyanHelsing committed Aug 1, 2018
2 parents 62e0cde + cb07abd commit 411e6ec
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions mfr/extensions/image/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ def __init__(self, *args, **kwargs):
def export(self):
parts = self.format.split('.')
type = parts[-1].lower()
max_size = [int(x) for x in parts[0].split('x')] if len(parts) == 2 else None
max_size = {
'w': None,
'h': None
}
if len(parts) == 2:
max_size['w'], max_size['h'] = [int(size) for size in parts[0].split('x')]
self.metrics.merge({
'type': type,
'max_size_w': max_size[0],
'max_size_h': max_size[1],
'max_size_w': max_size['w'],
'max_size_h': max_size['h'],
})
try:
if self.ext in ['.psd']:
Expand All @@ -36,13 +41,18 @@ def export(self):
else:
image = Image.open(self.source_file_path)

if max_size:
if not (max_size.get('w') or max_size.get('h')):
# If any of the dimensions for the resize aren't defined:
pass
else:
# resize the image to the w/h maximum specified
ratio = min(max_size[0] / image.size[0], max_size[1] / image.size[1])
ratio = min(max_size['w'] / image.size[0], max_size['h'] / image.size[1])
self.metrics.add('ratio', ratio)
if ratio < 1:
image = image.resize((round(image.size[0] * ratio),
round(image.size[1] * ratio)), Image.ANTIALIAS)
image = image.resize(
(round(image.size[0] * ratio), round(image.size[1] * ratio)),
Image.ANTIALIAS
)

# Mode 'P' is for paletted images. They must be converted to RGB before exporting to
# jpeg, otherwise Pillow will throw an error. This is a temporary workaround, as the
Expand Down

0 comments on commit 411e6ec

Please sign in to comment.