From 7228055bca2c0bfc00c2a923ed25124cb366b7d1 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Sun, 14 Dec 2025 23:44:30 -0800 Subject: [PATCH] reduce file operations in webtoon and file fusion (#1191) * reduce file operations in webtoon * reduce file operations of file fusion * fix file fusion failed to prepare * close webtoon image before remove * use temp directory --- kindlecomicconverter/comic2ebook.py | 28 ++++++++++++---------------- kindlecomicconverter/comic2panel.py | 21 +++++++++------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 8cb784f..357b67e 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -851,14 +851,16 @@ def mupdf_pdf_process_pages_parallel(filename, output_dir, target_height): -def getWorkFolder(afile): +def getWorkFolder(afile, workdir=None): + if not workdir: + workdir = mkdtemp('', 'KCC-') + fullPath = os.path.join(workdir, 'OEBPS', 'Images') + else: + fullPath = workdir if os.path.isdir(afile): if disk_usage(gettempdir())[2] < getDirectorySize(afile) * 2.5: raise UserWarning("Not enough disk space to perform conversion.") - workdir = mkdtemp('', 'KCC-', os.path.dirname(afile)) try: - os.rmdir(workdir) - fullPath = os.path.join(workdir, 'OEBPS', 'Images') copytree(afile, fullPath) sanitizePermissions(fullPath) return workdir @@ -869,8 +871,6 @@ def getWorkFolder(afile): if disk_usage(gettempdir())[2] < os.path.getsize(afile) * 2.5: raise UserWarning("Not enough disk space to perform conversion.") if afile.lower().endswith('.pdf'): - workdir = mkdtemp('', 'KCC-', os.path.dirname(afile)) - fullPath = os.path.join(workdir, 'OEBPS', 'Images') if not os.path.exists(fullPath): os.makedirs(fullPath) path = workdir @@ -887,8 +887,6 @@ def getWorkFolder(afile): raise UserWarning(f"Failed to extract images from PDF file. {e}") return workdir else: - workdir = mkdtemp('', 'KCC-', os.path.dirname(afile)) - fullPath = os.path.join(workdir, 'OEBPS', 'Images') if not os.path.exists(fullPath): os.makedirs(fullPath) try: @@ -1532,17 +1530,15 @@ def makeFusion(sources: List[str]): print(f"Processing {source}...") checkPre(source) print("Checking images...") - path = getWorkFolder(source) - pathfinder = os.path.join(path, "OEBPS", "Images") - sanitizeTree(pathfinder) - # TODO: remove flattenTree when subchapters are supported - flattenTree(pathfinder) source_path = Path(source) if source_path.is_file(): - os.renames(pathfinder, fusion_path.joinpath(source_path.stem)) + targetpath = fusion_path.joinpath(source_path.stem) else: - os.renames(pathfinder, fusion_path.joinpath(source_path.name)) - + targetpath = fusion_path.joinpath(source_path.name) + getWorkFolder(source, str(targetpath)) + sanitizeTree(targetpath) + # TODO: remove flattenTree when subchapters are supported + flattenTree(targetpath) end = perf_counter() print(f"makefusion: {end - start} seconds") diff --git a/kindlecomicconverter/comic2panel.py b/kindlecomicconverter/comic2panel.py index ccdbb06..f45cc49 100644 --- a/kindlecomicconverter/comic2panel.py +++ b/kindlecomicconverter/comic2panel.py @@ -67,13 +67,14 @@ def mergeDirectory(work): result = Image.new('RGB', (targetWidth, targetHeight)) y = 0 for i in imagesValid: - img = Image.open(i).convert('RGB') - if img.size[0] < targetWidth or img.size[0] > targetWidth: - widthPercent = (targetWidth / float(img.size[0])) - heightSize = int((float(img.size[1]) * float(widthPercent))) - img = ImageOps.fit(img, (targetWidth, heightSize), method=Image.BICUBIC, centering=(0.5, 0.5)) - result.paste(img, (0, y)) - y += img.size[1] + with Image.open(i) as img: + img = img.convert('RGB') + if img.size[0] < targetWidth or img.size[0] > targetWidth: + widthPercent = (targetWidth / float(img.size[0])) + heightSize = int((float(img.size[1]) * float(widthPercent))) + img = ImageOps.fit(img, (targetWidth, heightSize), method=Image.BICUBIC, centering=(0.5, 0.5)) + result.paste(img, (0, y)) + y += img.size[1] os.remove(i) savePath = os.path.split(imagesValid[0]) result.save(os.path.join(savePath[0], os.path.splitext(savePath[1])[0] + '.png'), 'PNG') @@ -253,10 +254,8 @@ def main(argv=None, job_progress='', qtgui=None): return 1 if args.height > 0: for sourceDir in args.input: - targetDir = sourceDir + "-Splitted" + targetDir = sourceDir if os.path.isdir(sourceDir): - rmtree(targetDir, True) - os.renames(sourceDir, targetDir) work = [] pagenumber = 1 splitWorkerOutput = [] @@ -313,8 +312,6 @@ def main(argv=None, job_progress='', qtgui=None): rmtree(targetDir, True) raise RuntimeError("One of workers crashed. Cause: " + splitWorkerOutput[0][0], splitWorkerOutput[0][1]) - if args.inPlace: - os.renames(targetDir, sourceDir) else: rmtree(targetDir, True) raise UserWarning("C2P: Source directory is empty.")