Skip to content

Commit 13a86fc

Browse files
committed
Add option that prevents aspect ratio changes
1 parent 638e817 commit 13a86fc

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/reCBZ/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ def main():
191191
dest="size_str",
192192
type=str,
193193
help="rescale images to the specified resolution")
194+
images_group.add_argument( "--keepratio",
195+
default=None,
196+
dest="keep_ratio",
197+
action="store_true",
198+
help="keep aspect ratio with --size")
194199
images_group.add_argument( "--noup", # rescale_group
195200
default=None,
196201
dest="no_upscale",

src/reCBZ/archive.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323
SOURCE_NAME:str = 'Source'
2424
chapter_prefix:str = 'v' # :) :D C:
2525

26+
def get_size_that_preserves_ratio(*, img_size, size_of_viewport):
27+
# derived from https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html#Image.thumbnail (HPND)
28+
import math
29+
def round_aspect(number, key):
30+
return max(min(math.floor(number), math.ceil(number), key=key), 1)
31+
32+
x, y = size_of_viewport
33+
34+
width, height = img_size
35+
36+
aspect = width / height
37+
if x / y >= aspect:
38+
x = round_aspect(y * aspect, key=lambda n: abs(aspect - n / y))
39+
else:
40+
y = round_aspect(
41+
x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n)
42+
)
43+
return x, y
44+
2645

2746
def write_zip(savepath, chapters):
2847
new_zip = ZipFile(savepath,'w')
@@ -128,13 +147,18 @@ def convert_page_worker(source, options, savedir=None):
128147
if page.landscape:
129148
new_size = new_size[::-1]
130149
n_width, n_height = new_size
150+
if options['keep_ratio']:
151+
resize_size = get_size_that_preserves_ratio(img_size=(width, height), size_of_viewport=new_size)
152+
log_buff += f'|trans: keeping ratio: size changed from {(width, height)} to {resize_size}\n'
153+
else:
154+
resize_size = new_size
131155
# downscaling
132156
if (width > n_width and height > n_height
133157
and not options['nodown']):
134-
img = img.resize((new_size), config.RESAMPLE_TYPE)
158+
img = img.resize(resize_size, config.RESAMPLE_TYPE)
135159
# upscaling
136160
elif not options['noup']:
137-
img = img.resize((new_size), config.RESAMPLE_TYPE)
161+
img = img.resize(resize_size, config.RESAMPLE_TYPE)
138162

139163
LossyFmt.quality = options['quality']
140164

@@ -248,6 +272,7 @@ def __init__(self, filename:str):
248272
self._page_opt['format'] = get_format_class(config.img_format)
249273
self._page_opt['quality'] = config.img_quality
250274
self._page_opt['size'] = config.img_size
275+
self._page_opt['keep_ratio'] = config.keep_ratio
251276
self._page_opt['grayscale'] = config.grayscale
252277
self._page_opt['noup'] = config.no_upscale
253278
self._page_opt['nodown'] = config.no_downscale

src/reCBZ/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
img_format:str = _cfg["image"]["img_format"]
3030
img_quality:int = _cfg["image"]["img_quality"]
3131
img_size:tuple = _cfg["image"]["img_size"]
32+
keep_ratio:bool = _cfg["image"]["keep_ratio"]
3233
no_upscale:bool = _cfg["image"]["no_upscale"]
3334
no_downscale:bool = _cfg["image"]["no_downscale"]
3435
grayscale:bool = _cfg["image"]["grayscale"]

src/reCBZ/defaults.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ img_format = ''
3535
img_quality = 80
3636
# new image width / height. set to 0,0 to preserve original dimensions
3737
img_size = [0,0]
38+
# preserve aspect ratio
39+
keep_ratio = false
3840
# set to True to disable upscaling of images smaller than resolution
3941
no_upscale = false
4042
# set to True to disable downscaling of images larger than resolution

0 commit comments

Comments
 (0)