Skip to content

Commit 9aed536

Browse files
committed
Add option that prevents aspect ratio changes
1 parent 638e817 commit 9aed536

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-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_when_downscaling",
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: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
SOURCE_NAME:str = 'Source'
2424
chapter_prefix:str = 'v' # :) :D C:
2525

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

2743
def write_zip(savepath, chapters):
2844
new_zip = ZipFile(savepath,'w')
@@ -128,13 +144,17 @@ def convert_page_worker(source, options, savedir=None):
128144
if page.landscape:
129145
new_size = new_size[::-1]
130146
n_width, n_height = new_size
147+
if options['keep_ratio']:
148+
resize_size = get_ratio_preserving_size(new_size)
149+
else:
150+
resize_size = new_size
131151
# downscaling
132152
if (width > n_width and height > n_height
133153
and not options['nodown']):
134-
img = img.resize((new_size), config.RESAMPLE_TYPE)
154+
img = img.resize((resize_size), config.RESAMPLE_TYPE)
135155
# upscaling
136156
elif not options['noup']:
137-
img = img.resize((new_size), config.RESAMPLE_TYPE)
157+
img = img.resize((resize_size), config.RESAMPLE_TYPE)
138158

139159
LossyFmt.quality = options['quality']
140160

@@ -248,6 +268,7 @@ def __init__(self, filename:str):
248268
self._page_opt['format'] = get_format_class(config.img_format)
249269
self._page_opt['quality'] = config.img_quality
250270
self._page_opt['size'] = config.img_size
271+
self._page_opt['keep_ratio'] = config.keep_ratio
251272
self._page_opt['grayscale'] = config.grayscale
252273
self._page_opt['noup'] = config.no_upscale
253274
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)