mirror of
https://github.com/ciromattia/kcc
synced 2026-06-03 05:03:26 +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 os.path.isdir(afile):
|
||||||
if disk_usage(gettempdir())[2] < getDirectorySize(afile) * 2.5:
|
if disk_usage(gettempdir())[2] < getDirectorySize(afile) * 2.5:
|
||||||
raise UserWarning("Not enough disk space to perform conversion.")
|
raise UserWarning("Not enough disk space to perform conversion.")
|
||||||
workdir = mkdtemp('', 'KCC-', os.path.dirname(afile))
|
|
||||||
try:
|
try:
|
||||||
os.rmdir(workdir)
|
|
||||||
fullPath = os.path.join(workdir, 'OEBPS', 'Images')
|
|
||||||
copytree(afile, fullPath)
|
copytree(afile, fullPath)
|
||||||
sanitizePermissions(fullPath)
|
sanitizePermissions(fullPath)
|
||||||
return workdir
|
return workdir
|
||||||
@@ -869,8 +871,6 @@ def getWorkFolder(afile):
|
|||||||
if disk_usage(gettempdir())[2] < os.path.getsize(afile) * 2.5:
|
if disk_usage(gettempdir())[2] < os.path.getsize(afile) * 2.5:
|
||||||
raise UserWarning("Not enough disk space to perform conversion.")
|
raise UserWarning("Not enough disk space to perform conversion.")
|
||||||
if afile.lower().endswith('.pdf'):
|
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):
|
if not os.path.exists(fullPath):
|
||||||
os.makedirs(fullPath)
|
os.makedirs(fullPath)
|
||||||
path = workdir
|
path = workdir
|
||||||
@@ -887,8 +887,6 @@ def getWorkFolder(afile):
|
|||||||
raise UserWarning(f"Failed to extract images from PDF file. {e}")
|
raise UserWarning(f"Failed to extract images from PDF file. {e}")
|
||||||
return workdir
|
return workdir
|
||||||
else:
|
else:
|
||||||
workdir = mkdtemp('', 'KCC-', os.path.dirname(afile))
|
|
||||||
fullPath = os.path.join(workdir, 'OEBPS', 'Images')
|
|
||||||
if not os.path.exists(fullPath):
|
if not os.path.exists(fullPath):
|
||||||
os.makedirs(fullPath)
|
os.makedirs(fullPath)
|
||||||
try:
|
try:
|
||||||
@@ -1532,17 +1530,15 @@ def makeFusion(sources: List[str]):
|
|||||||
print(f"Processing {source}...")
|
print(f"Processing {source}...")
|
||||||
checkPre(source)
|
checkPre(source)
|
||||||
print("Checking images...")
|
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)
|
source_path = Path(source)
|
||||||
if source_path.is_file():
|
if source_path.is_file():
|
||||||
os.renames(pathfinder, fusion_path.joinpath(source_path.stem))
|
targetpath = fusion_path.joinpath(source_path.stem)
|
||||||
else:
|
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()
|
end = perf_counter()
|
||||||
print(f"makefusion: {end - start} seconds")
|
print(f"makefusion: {end - start} seconds")
|
||||||
|
|||||||
@@ -67,13 +67,14 @@ def mergeDirectory(work):
|
|||||||
result = Image.new('RGB', (targetWidth, targetHeight))
|
result = Image.new('RGB', (targetWidth, targetHeight))
|
||||||
y = 0
|
y = 0
|
||||||
for i in imagesValid:
|
for i in imagesValid:
|
||||||
img = Image.open(i).convert('RGB')
|
with Image.open(i) as img:
|
||||||
if img.size[0] < targetWidth or img.size[0] > targetWidth:
|
img = img.convert('RGB')
|
||||||
widthPercent = (targetWidth / float(img.size[0]))
|
if img.size[0] < targetWidth or img.size[0] > targetWidth:
|
||||||
heightSize = int((float(img.size[1]) * float(widthPercent)))
|
widthPercent = (targetWidth / float(img.size[0]))
|
||||||
img = ImageOps.fit(img, (targetWidth, heightSize), method=Image.BICUBIC, centering=(0.5, 0.5))
|
heightSize = int((float(img.size[1]) * float(widthPercent)))
|
||||||
result.paste(img, (0, y))
|
img = ImageOps.fit(img, (targetWidth, heightSize), method=Image.BICUBIC, centering=(0.5, 0.5))
|
||||||
y += img.size[1]
|
result.paste(img, (0, y))
|
||||||
|
y += img.size[1]
|
||||||
os.remove(i)
|
os.remove(i)
|
||||||
savePath = os.path.split(imagesValid[0])
|
savePath = os.path.split(imagesValid[0])
|
||||||
result.save(os.path.join(savePath[0], os.path.splitext(savePath[1])[0] + '.png'), 'PNG')
|
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
|
return 1
|
||||||
if args.height > 0:
|
if args.height > 0:
|
||||||
for sourceDir in args.input:
|
for sourceDir in args.input:
|
||||||
targetDir = sourceDir + "-Splitted"
|
targetDir = sourceDir
|
||||||
if os.path.isdir(sourceDir):
|
if os.path.isdir(sourceDir):
|
||||||
rmtree(targetDir, True)
|
|
||||||
os.renames(sourceDir, targetDir)
|
|
||||||
work = []
|
work = []
|
||||||
pagenumber = 1
|
pagenumber = 1
|
||||||
splitWorkerOutput = []
|
splitWorkerOutput = []
|
||||||
@@ -313,8 +312,6 @@ def main(argv=None, job_progress='', qtgui=None):
|
|||||||
rmtree(targetDir, True)
|
rmtree(targetDir, True)
|
||||||
raise RuntimeError("One of workers crashed. Cause: " + splitWorkerOutput[0][0],
|
raise RuntimeError("One of workers crashed. Cause: " + splitWorkerOutput[0][0],
|
||||||
splitWorkerOutput[0][1])
|
splitWorkerOutput[0][1])
|
||||||
if args.inPlace:
|
|
||||||
os.renames(targetDir, sourceDir)
|
|
||||||
else:
|
else:
|
||||||
rmtree(targetDir, True)
|
rmtree(targetDir, True)
|
||||||
raise UserWarning("C2P: Source directory is empty.")
|
raise UserWarning("C2P: Source directory is empty.")
|
||||||
|
|||||||
Reference in New Issue
Block a user