Skip to content

Commit

Permalink
Add option that prevents aspect ratio changes when downscaling
Browse files Browse the repository at this point in the history
  • Loading branch information
snarkyyy committed Feb 28, 2023
1 parent 638e817 commit 1a6aab5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/reCBZ/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ def main():
dest="size_str",
type=str,
help="rescale images to the specified resolution")
images_group.add_argument( "--keepratio",
default=None,
dest="keep_ratio_when_downscaling",
action="store_true",
help="keep aspect ratio with --size")
images_group.add_argument( "--noup", # rescale_group
default=None,
dest="no_upscale",
Expand Down
25 changes: 23 additions & 2 deletions src/reCBZ/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
SOURCE_NAME:str = 'Source'
chapter_prefix:str = 'v' # :) :D C:

def get_ratio_preserving_size(provided_size):
# derived from https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html#Image.thumbnail (HPND)
def round_aspect(number, key):
return max(min(math.floor(number), math.ceil(number), key=key), 1)

x, y = provided_size

aspect = self.width / self.height
if x / y >= aspect:
x = round_aspect(y * aspect, key=lambda n: abs(aspect - n / y))
else:
y = round_aspect(
x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n)
)
return x, y


def write_zip(savepath, chapters):
new_zip = ZipFile(savepath,'w')
Expand Down Expand Up @@ -128,13 +144,17 @@ def convert_page_worker(source, options, savedir=None):
if page.landscape:
new_size = new_size[::-1]
n_width, n_height = new_size
if options['keep_ratio']:
resize_size = get_ratio_preserving_size(new_size)
else:
resize_size = new_size
# downscaling
if (width > n_width and height > n_height
and not options['nodown']):
img = img.resize((new_size), config.RESAMPLE_TYPE)
img = img.resize((resize_size), config.RESAMPLE_TYPE)
# upscaling
elif not options['noup']:
img = img.resize((new_size), config.RESAMPLE_TYPE)
img = img.resize((resize_size), config.RESAMPLE_TYPE)

LossyFmt.quality = options['quality']

Expand Down Expand Up @@ -248,6 +268,7 @@ def __init__(self, filename:str):
self._page_opt['format'] = get_format_class(config.img_format)
self._page_opt['quality'] = config.img_quality
self._page_opt['size'] = config.img_size
self._page_opt['keep_ratio'] = config.keep_ratio
self._page_opt['grayscale'] = config.grayscale
self._page_opt['noup'] = config.no_upscale
self._page_opt['nodown'] = config.no_downscale
Expand Down
1 change: 1 addition & 0 deletions src/reCBZ/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
img_format:str = _cfg["image"]["img_format"]
img_quality:int = _cfg["image"]["img_quality"]
img_size:tuple = _cfg["image"]["img_size"]
keep_ratio:bool = _cfg["image"]["keep_ratio"]
no_upscale:bool = _cfg["image"]["no_upscale"]
no_downscale:bool = _cfg["image"]["no_downscale"]
grayscale:bool = _cfg["image"]["grayscale"]
Expand Down
2 changes: 2 additions & 0 deletions src/reCBZ/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ img_format = ''
img_quality = 80
# new image width / height. set to 0,0 to preserve original dimensions
img_size = [0,0]
# preserve aspect ratio
keep_ratio = false
# set to True to disable upscaling of images smaller than resolution
no_upscale = false
# set to True to disable downscaling of images larger than resolution
Expand Down

0 comments on commit 1a6aab5

Please sign in to comment.