diff --git a/cruncher/__init__.py b/cruncher/__init__.py index 955b3f3..805a39c 100644 --- a/cruncher/__init__.py +++ b/cruncher/__init__.py @@ -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() @@ -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', @@ -108,12 +119,12 @@ # 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: @@ -121,8 +132,8 @@ def cli(image, directory, output, file_format, quality, size, append, 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() diff --git a/cruncher/core.py b/cruncher/core.py index 1a9b9da..8c13ff6 100644 --- a/cruncher/core.py +++ b/cruncher/core.py @@ -26,7 +26,7 @@ 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 @@ -34,9 +34,10 @@ def __init__(self, image, directory, output, file_format, quality, size, 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 = [] @@ -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 @@ -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 @@ -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) diff --git a/cruncher/cruncher.py b/cruncher/cruncher.py index b9d05f2..c08f99e 100644 --- a/cruncher/cruncher.py +++ b/cruncher/cruncher.py @@ -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}" @@ -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