Skip to content

Commit

Permalink
Added #4 --keep-aspect and changed --ignore-orientation to --keep-ori…
Browse files Browse the repository at this point in the history
…entation to be consistent
  • Loading branch information
danielmorell committed Jan 10, 2020
1 parent a76a3a4 commit c11b518
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
31 changes: 21 additions & 10 deletions cruncher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Local Imports
from .core import CrunchHandler, OUTPUT_FILE_FORMATS, ERROR_HANDLING

__version__ = '0.2.2'
__version__ = '0.3.0'


@click.command()
Expand Down Expand Up @@ -64,10 +64,21 @@
help='Append a string to the filename.'
)
@click.option(
'--ignore-orientation', 'orientation',
'--keep-orientation', 'orientation',
is_flag=True,
help='Include this flag to ignore the original image orientation. I.e. if '
'--size is landscape all portrait images will be cropped as landscape.'
help='Include this flag to keep the original image orientation. I.e. if '
'--size is landscape all portrait images will be resized as portrait, '
'and all landscape will be resized as landscape. If this parameter is '
'not included portrait images will become landscape, and vice versa.'
)
@click.option(
'--keep-aspect', 'aspect',
is_flag=True,
help='Include this flag to keep the original image aspect ratio. If this '
'parameter is included the image will be scaled but the aspect '
'ratio will be retained. This should be used with --keep-orientation '
'otherwise the aspect ratio may be scaled as landscape when the '
'original is portrait.'
)
@click.option(
'-m', '--keep-metadata', 'metadata',
Expand Down Expand Up @@ -108,21 +119,21 @@
# help='Specify how Cruncher should handle errors.'
# )
def cli(image, directory, output, file_format, quality, size, append,
metadata, orientation, nversions, recursive, config, version):
orientation, aspect, metadata, nversions, recursive, config, version):
"""
Cruncher 0.2.2
Cruncher 0.3.0
This is a simple CLI image optimization wrapper for the Python Image
Library fork Pillow. Cruncher takes images and scales them to
This is a simple yet powerful CLI image optimization wrapper for the
Python Image Library fork Pillow. Cruncher takes images and scales them to
the specified size and quality.
"""
if version is not None:
click.echo('Cruncher ' + __version__)
return

cruncher = CrunchHandler(image=image, directory=directory, output=output, file_format=file_format,
quality=quality, size=size, append=append, metadata=metadata,
orientation=orientation, nversions=nversions, recursive=recursive,
quality=quality, size=size, append=append, orientation=orientation,
metadata=metadata, aspect=aspect, nversions=nversions, recursive=recursive,
config=config)
cruncher.run_cruncher()

Expand Down
10 changes: 7 additions & 3 deletions cruncher/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@
class CrunchHandler:

def __init__(self, image, directory, output, file_format, quality, size,
append, metadata, orientation, nversions, recursive, config):
append, orientation, aspect, metadata, nversions, recursive, config):
self.image = image
self.directory = directory
self.output = output
self.file_format = file_format
self.quality = quality
self.size = size
self.append = append
self.orientation = orientation
self.aspect = aspect
self.metadata = metadata
self.settings = {}
self.orientation = orientation
self.nversions = nversions
self.recursive = recursive
self.versions = []
Expand Down Expand Up @@ -76,7 +77,8 @@ def generate_versions(self):
'height': size[1],
'quality': click.prompt(f'Quality', type=click.IntRange(1, 100, clamp=True), default=80),
'append': click.prompt(f'Append filename', type=str, default='', show_default=False),
'orientation': click.prompt(f'Ignore orientation', type=bool, default=False),
'aspect': click.prompt(f'Keep aspect', type=bool, default=False),
'orientation': click.prompt(f'Keep orientation', type=bool, default=False),
'metadata': click.prompt(f'Keep Metadata', type=bool, default=False),
})
i += 1
Expand All @@ -94,6 +96,7 @@ def generate_versions(self):
'height': self.parse_size(self.size)[1],
'quality': self.quality,
'append': self.append,
'aspect': self.aspect,
'orientation': self.orientation,
'metadata': self.metadata,
'subsampling': -1
Expand Down Expand Up @@ -163,6 +166,7 @@ def parse_json_configs(self, configs):
'height': self.get_config(version, 'height'),
'append': self.get_config(version, 'append'),
'orientation': self.get_config(version, 'keep_orientation', False),
'aspect': self.get_config(version, 'keep_aspect', False),
'metadata': self.get_config(version, 'keep_metadata', False),
'subsampling': self.get_config(version, 'subsampling', None),
'icc_conversion': self.get_config(version, 'icc_conversion', None)
Expand Down
21 changes: 14 additions & 7 deletions cruncher/cruncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def generate_filename(self, filename, version):
if not version['append']:
version['append'] = ''
filename = filename.split('.')
del(filename[-1])
del (filename[-1])
filename = '.'.join(filename)
return f"{filename}{version['append']}.{self.format}"

Expand All @@ -88,18 +88,25 @@ def resize(self):
if size == (None, None):
return image

old_aspect = image.width / image.height
new_aspect = size[0] / size[1]
old_orientation = image.width / image.height
new_orientation = size[0] / size[1]

if version['orientation'] and (old_aspect <= 1 < new_aspect or old_aspect >= 1 > new_aspect):
if (version['orientation']
and (old_orientation <= 1 < new_orientation or old_orientation >= 1 > new_orientation)):
# if one is horizontal and the other vertical flip the orientation
size = (size[1], size[0])
new_aspect = size[0] / size[1]
new_orientation = size[0] / size[1]

if size == (image.width, image.height):
return image
self.image = image
return

if version['aspect']:
image.thumbnail(size, Image.LANCZOS)
self.image = image
return

if new_aspect < old_aspect:
if new_orientation < old_orientation:
v_width = image.width - (image.height / size[1] * size[0])
v_height = image.height
left = v_width / 2
Expand Down

0 comments on commit c11b518

Please sign in to comment.