Add inplace parameter, fix resizing image error.
This commit is contained in:
parent
90c889fb77
commit
6fc74ef755
1 changed files with 22 additions and 6 deletions
|
@ -30,6 +30,10 @@ from PIL import Image
|
||||||
logger = structlog.get_logger(__name__)
|
logger = structlog.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def maxsize_type(value):
|
||||||
|
return list(map(int, value.split("x")))
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="cbz-format",
|
prog="cbz-format",
|
||||||
|
@ -38,10 +42,17 @@ def parse_args():
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-s",
|
"-s",
|
||||||
"--max-size",
|
"--max-size",
|
||||||
type=lambda ms: ms.split("x"),
|
type=maxsize_type,
|
||||||
help="maximum `widthxheight` sizes for images.",
|
help="maximum `widthxheight` sizes for images.",
|
||||||
)
|
)
|
||||||
parser.add_argument("--log", choices=["DEBUG", "INFO"], default="info")
|
parser.add_argument("--log", choices=["DEBUG", "INFO"], default="info")
|
||||||
|
parser.add_argument(
|
||||||
|
"-i",
|
||||||
|
"--inplace",
|
||||||
|
default=False,
|
||||||
|
action="store_true",
|
||||||
|
help="Erase original cbz file",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-q", "--quality", type=int, default=75, help="Quality of generated jpeg images"
|
"-q", "--quality", type=int, default=75, help="Quality of generated jpeg images"
|
||||||
)
|
)
|
||||||
|
@ -49,7 +60,7 @@ def parse_args():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def operate_cbz(cbz_path, args, max_size=None):
|
def operate_cbz(cbz_path, quality=75, max_size=None, inplace=False):
|
||||||
if not cbz_path.exists():
|
if not cbz_path.exists():
|
||||||
logger.error(f"File {cbz_path} doesn't exist")
|
logger.error(f"File {cbz_path} doesn't exist")
|
||||||
return
|
return
|
||||||
|
@ -59,7 +70,7 @@ def operate_cbz(cbz_path, args, max_size=None):
|
||||||
shutil.unpack_archive(cbz_path, tmp_dir, "zip")
|
shutil.unpack_archive(cbz_path, tmp_dir, "zip")
|
||||||
for img_path in tmp_dir.iterdir():
|
for img_path in tmp_dir.iterdir():
|
||||||
if not img_path.is_file() or not re.match(
|
if not img_path.is_file() or not re.match(
|
||||||
r".*\.(png|webp)$", str(img_path)
|
r".*\.(png|webp|jpe?g)$", str(img_path), re.I
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
with Image.open(img_path) as im:
|
with Image.open(img_path) as im:
|
||||||
|
@ -71,18 +82,23 @@ def operate_cbz(cbz_path, args, max_size=None):
|
||||||
str(img_path.parent / img_path.stem) + ".jpg",
|
str(img_path.parent / img_path.stem) + ".jpg",
|
||||||
"JPEG",
|
"JPEG",
|
||||||
optimize=True,
|
optimize=True,
|
||||||
quality=args.quality,
|
quality=quality,
|
||||||
)
|
)
|
||||||
img_path.unlink()
|
img_path.unlink()
|
||||||
output_path_stem = str(tmp_dir.parent / cbz_path.stem) + "_copy"
|
output_path_stem = str(tmp_dir.parent / cbz_path.stem)
|
||||||
|
if not inplace:
|
||||||
|
output_path_stem += "_copy"
|
||||||
shutil.make_archive(output_path_stem, "zip", tmp_dir)
|
shutil.make_archive(output_path_stem, "zip", tmp_dir)
|
||||||
pathlib.Path(output_path_stem + ".zip").rename(output_path_stem + ".cbz")
|
pathlib.Path(output_path_stem + ".zip").rename(output_path_stem + ".cbz")
|
||||||
|
logger.info(f"Created {output_path_stem + ".cbz"} file")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
for f in args.files:
|
for f in args.files:
|
||||||
operate_cbz(f, args)
|
operate_cbz(
|
||||||
|
f, quality=args.quality, max_size=args.max_size, inplace=args.inplace
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue