cbz2ebook: Pad to screen size

This avoids issues with my Kobo Forma, which displays full-width pages
at an offset, if it is preceded by a page which is less wide.
This commit is contained in:
Simon Bruder 2024-02-19 21:10:53 +01:00
parent f59ab31d54
commit 88b1d14273
Signed by: simon
GPG key ID: 347FF8699CDA0776

View file

@ -1,33 +1,13 @@
#!/usr/bin/env cached-nix-shell #!/usr/bin/env cached-nix-shell
#!nix-shell -i python3 -p python3 python3Packages.pillow python3Packages.tqdm #!nix-shell -i python3 -p python3 python3Packages.pillow python3Packages.tqdm
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
from zipfile import ZipFile
import argparse import argparse
import sys import sys
import zlib import zlib
from concurrent.futures import ThreadPoolExecutor
from zipfile import ZipFile
from PIL import Image, ImageOps
def should_scale(in_width, in_height, out_width, out_height): from tqdm import tqdm
return not (
(in_width == out_width and in_height <= out_height)
or (in_height == out_height and in_width <= out_width)
)
def scaled_size(in_width, in_height, out_width, out_height):
in_aspect_ratio = float(in_width) / float(in_height)
out_aspect_ratio = float(out_width) / float(out_height)
if in_aspect_ratio > out_aspect_ratio:
height = out_width / in_aspect_ratio
width = out_width
else:
width = out_height * in_aspect_ratio
height = out_height
return int(round(width)), round(int(height))
def process_image(im): def process_image(im):
@ -38,12 +18,8 @@ def process_image(im):
# convert to greyscale # convert to greyscale
im = im.convert("L") im = im.convert("L")
# resize if necessary # pad to screen size
if should_scale(im.width, im.height, args.width, args.height): im = ImageOps.pad(im, (args.width, args.height), Image.Resampling.LANCZOS, "#fff")
im = im.resize(
scaled_size(im.width, im.height, args.width, args.height),
resample=Image.Resampling.LANCZOS,
)
return im return im