Add script to convert images from cbz to jpeg
This commit is contained in:
parent
2b0be705d0
commit
e87d41a21f
1 changed files with 84 additions and 0 deletions
84
prog/cbz-format.py
Normal file
84
prog/cbz-format.py
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""Program to convert images from cbz.
|
||||||
|
|
||||||
|
It converts png and webp to jpeg.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# /// script
|
||||||
|
# requires-python = ">=3.11"
|
||||||
|
# dependencies = [
|
||||||
|
# "pillow>=10.4,<10.5",
|
||||||
|
# "structlog>=24.4,<24.5",
|
||||||
|
# ]
|
||||||
|
# ///
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import structlog
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
logger = structlog.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
prog="cbz-format",
|
||||||
|
description="Convert image from cbz file to jpeg and resize them.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--max-size",
|
||||||
|
type=lambda ms: ms.split("x"),
|
||||||
|
help="maximum `widthxheight` sizes for images.",
|
||||||
|
)
|
||||||
|
parser.add_argument("--log", choices=["DEBUG", "INFO"], default="info")
|
||||||
|
parser.add_argument(
|
||||||
|
"-q", "--quality", type=int, default=75, help="Quality of generated jpeg images"
|
||||||
|
)
|
||||||
|
parser.add_argument("files", nargs="+", type=pathlib.Path, help="Path of cbz files")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def operate_cbz(cbz_path, args, max_size=None):
|
||||||
|
if not cbz_path.exists():
|
||||||
|
logger.error(f"File {cbz_path} doesn't exist")
|
||||||
|
return
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmp_dir_:
|
||||||
|
tmp_dir = pathlib.Path(tmp_dir_)
|
||||||
|
shutil.unpack_archive(cbz_path, tmp_dir, "zip")
|
||||||
|
for img_path in tmp_dir.iterdir():
|
||||||
|
if not img_path.is_file() or not re.match(
|
||||||
|
r".*\.(png|webp)$", str(img_path)
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
with Image.open(img_path) as im:
|
||||||
|
if max_size:
|
||||||
|
im.thumbnail(max_size)
|
||||||
|
if im.mode != "RGB":
|
||||||
|
im = im.convert("RGB")
|
||||||
|
im.save(
|
||||||
|
str(img_path.parent / img_path.stem) + ".jpg",
|
||||||
|
"JPEG",
|
||||||
|
optimize=True,
|
||||||
|
quality=args.quality,
|
||||||
|
)
|
||||||
|
img_path.unlink()
|
||||||
|
output_path_stem = str(tmp_dir.parent / cbz_path.stem) + "_copy"
|
||||||
|
shutil.make_archive(output_path_stem, "zip", tmp_dir)
|
||||||
|
pathlib.Path(output_path_stem + ".zip").rename(output_path_stem + ".cbz")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args()
|
||||||
|
for f in args.files:
|
||||||
|
operate_cbz(f, args)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue