Compare commits
2 commits
2b5735a380
...
2ad7f3a52e
Author | SHA1 | Date | |
---|---|---|---|
Simon Bruder | 2ad7f3a52e | ||
Simon Bruder | bcfa304862 |
53
cbz2ebook.py
53
cbz2ebook.py
|
@ -5,8 +5,6 @@ from concurrent.futures import ThreadPoolExecutor
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
|
||||||
import zlib
|
|
||||||
|
|
||||||
|
|
||||||
def should_scale(in_width, in_height, out_width, out_height):
|
def should_scale(in_width, in_height, out_width, out_height):
|
||||||
|
@ -48,31 +46,6 @@ def process_image(im):
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
|
||||||
def process_archive(srczip, dstzip, multithread=True):
|
|
||||||
executor = ThreadPoolExecutor()
|
|
||||||
|
|
||||||
if multithread:
|
|
||||||
mapper = executor.map
|
|
||||||
else:
|
|
||||||
mapper = map
|
|
||||||
|
|
||||||
srcfiles = sorted(
|
|
||||||
map(
|
|
||||||
lambda zi: zi.filename,
|
|
||||||
filter(lambda zi: not zi.is_dir(), srczip.infolist()),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
for idx, im in enumerate(
|
|
||||||
tqdm(
|
|
||||||
mapper(process_image, mapper(Image.open, mapper(srczip.open, srcfiles))),
|
|
||||||
total=len(srcfiles),
|
|
||||||
)
|
|
||||||
):
|
|
||||||
with dstzip.open(f"{idx+1:04d}.jpg", "w") as outpagefile:
|
|
||||||
im.save(outpagefile, format="JPEG", quality=92)
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("infile")
|
parser.add_argument("infile")
|
||||||
parser.add_argument("outfile")
|
parser.add_argument("outfile")
|
||||||
|
@ -84,14 +57,22 @@ args = parser.parse_args()
|
||||||
srczip = ZipFile(args.infile)
|
srczip = ZipFile(args.infile)
|
||||||
dstzip = ZipFile(args.outfile, "x")
|
dstzip = ZipFile(args.outfile, "x")
|
||||||
|
|
||||||
|
srcfiles = sorted(
|
||||||
|
map(lambda zi: zi.filename, filter(lambda zi: not zi.is_dir(), srczip.infolist(),),)
|
||||||
|
)
|
||||||
|
|
||||||
|
executor = ThreadPoolExecutor()
|
||||||
|
|
||||||
if args.singlethreaded:
|
if args.singlethreaded:
|
||||||
process_archive(srczip, dstzip, multithread=False)
|
mapper = map
|
||||||
else:
|
else:
|
||||||
try:
|
mapper = executor.map
|
||||||
process_archive(srczip, dstzip, multithread=True)
|
|
||||||
except zlib.error as e:
|
for idx, im in enumerate(
|
||||||
print(f"zlib choked ({e}), retrying in single threaded mode", file=sys.stderr)
|
tqdm(
|
||||||
# replace partial archive with new empty one
|
mapper(process_image, mapper(Image.open, mapper(srczip.open, srcfiles)),),
|
||||||
dstzip.close()
|
total=len(srcfiles),
|
||||||
dstzip = ZipFile(args.outfile, "w")
|
)
|
||||||
process_archive(srczip, dstzip, multithread=False)
|
):
|
||||||
|
with dstzip.open(f"{idx+1:04d}.jpg", "w") as outpagefile:
|
||||||
|
im.save(outpagefile, format="JPEG", quality=92)
|
||||||
|
|
Loading…
Reference in a new issue