From 62ffa2bc8042dc89d56609d31d44071c52eff9de Mon Sep 17 00:00:00 2001 From: utopiafallen Date: Mon, 3 Feb 2025 21:44:12 -0800 Subject: [PATCH] Improve processing performance by partially undoing "Refactored detection of corrupted files" Commit f9526349715709e808e50d1212477847f275ea62 moved image corruption detection out from the ComicPage constructor and into a standalone detectCorruption() function. This led to a performance regression because now corruption detection happens in a single thread when it used to be distributed across worker threads, and because a source image is now loaded twice in memory: once during corruption detection and once when actually going to process the image. Image file corruption detection is now back inside the ComicPage constructor and the extra load() has been removed because the convert() call will automatically invoke load() and most likely throw the same exceptions. --- kindlecomicconverter/comic2ebook.py | 8 ++------ kindlecomicconverter/image.py | 8 +++++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 02eed1c..ed2d9f2 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -888,8 +888,7 @@ def chunk_process(path, mode, parent): firstTome = False return output - -def detectCorruption(tmppath, orgpath): +def detectSuboptimalProcessing(tmppath, orgpath): imageNumber = 0 imageSmaller = 0 alreadyProcessed = False @@ -905,9 +904,6 @@ def detectCorruption(tmppath, orgpath): raise RuntimeError('Image file %s is corrupted.' % pathOrg) try: img = Image.open(path) - img.verify() - img = Image.open(path) - img.load() imageNumber += 1 if options.profileData[1][0] > img.size[0] and options.profileData[1][1] > img.size[1]: imageSmaller += 1 @@ -1179,7 +1175,7 @@ def makeBook(source, qtgui=None): path = getWorkFolder(source) print("Checking images...") getComicInfo(os.path.join(path, "OEBPS", "Images"), source) - detectCorruption(os.path.join(path, "OEBPS", "Images"), source) + detectSuboptimalProcessing(os.path.join(path, "OEBPS", "Images"), source) if options.webtoon: y = image.ProfileData.Profiles[options.profile][1][1] comic2panel.main(['-y ' + str(y), '-i', '-m', path], qtgui) diff --git a/kindlecomicconverter/image.py b/kindlecomicconverter/image.py index 91d0a8a..4398c89 100755 --- a/kindlecomicconverter/image.py +++ b/kindlecomicconverter/image.py @@ -141,7 +141,13 @@ class ComicPageParser: self.source = source self.size = self.opt.profileData[1] self.payload = [] - self.image = Image.open(os.path.join(source[0], source[1])).convert('RGB') + + # Detect corruption in source image, let caller catch any exceptions triggered. + srcImgPath = os.path.join(source[0], source[1]) + self.image = Image.open(srcImgPath) + self.image.verify() + self.image = Image.open(srcImgPath).convert('RGB') + self.color = self.colorCheck() self.fill = self.fillCheck() # backwards compatibility for Pillow >9.1.0