From 4bb6ba55d34a87db11bfc743ce028c97d637f975 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Sun, 8 Jun 2025 11:08:57 -0700 Subject: [PATCH] cover has minimal processing and is shared across splits (#953) * refactor cover handling * skip cover processing * rename cover to cover_path * fix scribe mobi detection * make things closer * rename save to save_to_epub --- kindlecomicconverter/comic2ebook.py | 36 +++++++++++++---------------- kindlecomicconverter/image.py | 26 +++++++++++++-------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 8c8dd83..caff636 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -314,12 +314,8 @@ def buildOPF(dstdir, title, filelist, cover=None): "\n"]) if cover is not None: - filename = getImageFileName(cover.replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\')) - if '.png' == filename[1]: - mt = 'image/png' - else: - mt = 'image/jpeg' - f.write("\n") reflist = [] for path in filelist: @@ -427,10 +423,9 @@ def buildOPF(dstdir, title, filelist, cover=None): ""]) f.close() -def buildEPUB(path, chapternames, tomenumber, ischunked): +def buildEPUB(path, chapternames, tomenumber, ischunked, cover: image.Cover): filelist = [] chapterlist = [] - cover = None os.mkdir(os.path.join(path, 'OEBPS', 'Text')) f = open(os.path.join(path, 'OEBPS', 'Text', 'style.css'), 'w', encoding='UTF-8') f.writelines(["@page {\n", @@ -508,18 +503,14 @@ def buildEPUB(path, chapternames, tomenumber, ischunked): "}\n"]) f.close() build_html_start = perf_counter() + cover.save_to_epub(os.path.join(path, 'OEBPS', 'Images', 'cover.jpg'), tomenumber) + options.covers.append((cover, options.uuid)) for dirpath, dirnames, filenames in os.walk(os.path.join(path, 'OEBPS', 'Images')): chapter = False dirnames, filenames = walkSort(dirnames, filenames) for afile in filenames: - if cover is None: - try: - cover = os.path.join(os.path.join(path, 'OEBPS', 'Images'), - 'cover' + getImageFileName(afile)[1]) - except Exception as e: - raise UserWarning(f"{afile}: {e}") - options.covers.append((image.Cover(os.path.join(dirpath, afile), cover, options, - tomenumber), options.uuid)) + if afile == 'cover.jpg': + continue if not chapter: chapterlist.append((dirpath.replace('Images', 'Text'), afile)) chapter = True @@ -801,6 +792,7 @@ def getPanelViewSize(deviceres, size): def sanitizeTree(filetree): chapterNames = {} page = 1 + cover_path = None for root, dirs, files in os.walk(filetree): dirs.sort(key=OS_SORT_KEY) files.sort(key=OS_SORT_KEY) @@ -815,6 +807,8 @@ def sanitizeTree(filetree): key = os.path.join(root, name) if key != newKey: os.replace(key, newKey) + if not cover_path: + cover_path = newKey for i, name in enumerate(dirs): tmpName = name slugified = slugify(name) @@ -826,7 +820,7 @@ def sanitizeTree(filetree): if key != newKey: os.replace(key, newKey) dirs[i] = newKey - return chapterNames + return chapterNames, cover_path def flattenTree(filetree): @@ -1242,7 +1236,9 @@ def makeBook(source, qtgui=None): print("Checking images...") getComicInfo(os.path.join(path, "OEBPS", "Images"), source) detectSuboptimalProcessing(os.path.join(path, "OEBPS", "Images"), source) - chapterNames = sanitizeTree(os.path.join(path, 'OEBPS', 'Images')) + chapterNames, cover_path = sanitizeTree(os.path.join(path, 'OEBPS', 'Images')) + cover = image.Cover(cover_path, options) + if options.webtoon: y = image.ProfileData.Profiles[options.profile][1][1] comic2panel.main(['-y ' + str(y), '-i', '-m', path], qtgui) @@ -1288,10 +1284,10 @@ def makeBook(source, qtgui=None): else: print("Creating EPUB file...") if len(tomes) > 1: - buildEPUB(tome, chapterNames, tomeNumber, True) + buildEPUB(tome, chapterNames, tomeNumber, True, cover) filepath.append(getOutputFilename(source, options.output, '.epub', ' ' + str(tomeNumber))) else: - buildEPUB(tome, chapterNames, tomeNumber, False) + buildEPUB(tome, chapterNames, tomeNumber, False, cover) filepath.append(getOutputFilename(source, options.output, '.epub', '')) makeZIP(tome + '_comic', tome, True) copyfile(tome + '_comic.zip', filepath[-1]) diff --git a/kindlecomicconverter/image.py b/kindlecomicconverter/image.py index f30fd00..e3763e7 100755 --- a/kindlecomicconverter/image.py +++ b/kindlecomicconverter/image.py @@ -414,14 +414,9 @@ class ComicPage: self.image = crop_empty_inter_panel(self.image, direction, background_color=self.fill) class Cover: - def __init__(self, source, target, opt, tomeid): + def __init__(self, source, opt): self.options = opt self.source = source - self.target = target - if tomeid == 0: - self.tomeid = 1 - else: - self.tomeid = tomeid self.image = Image.open(source) # backwards compatibility for Pillow >9.1.0 if not hasattr(Image, 'Resampling'): @@ -433,6 +428,15 @@ class Cover: self.image = ImageOps.autocontrast(self.image) if not self.options.forcecolor: self.image = self.image.convert('L') + self.crop_main_cover() + + size = list(self.options.profileData[1]) + if self.options.profile == 'KS': + if 'MOBI' in self.options.format or 'EPUB' in self.options.format: + size[1] = min(size[1], 1920) + self.image.thumbnail(tuple(size), Image.Resampling.LANCZOS) + + def crop_main_cover(self): w, h = self.image.size if w / h > 2: if self.options.righttoleft: @@ -444,12 +448,14 @@ class Cover: self.image = self.image.crop((0, 0, w/2 - w * 0.03, h)) else: self.image = self.image.crop((w/2 + w * 0.03, 0, w, h)) - self.image.thumbnail(self.options.profileData[1], Image.Resampling.LANCZOS) - self.save() - def save(self): + def save_to_epub(self, target, tomeid): + if tomeid == 0: + self.tomeid = 1 + else: + self.tomeid = tomeid try: - self.image.save(self.target, "JPEG", optimize=1, quality=85) + self.image.save(target, "JPEG", optimize=1, quality=85) except IOError: raise RuntimeError('Failed to save cover.')