mirror of
https://github.com/ciromattia/kcc
synced 2026-01-25 22:47:28 +00:00
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
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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.")
|
||||
|
||||
Reference in New Issue
Block a user