mirror of
https://github.com/ciromattia/kcc
synced 2025-12-13 01:36:27 +00:00
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
This commit is contained in:
@@ -314,12 +314,8 @@ def buildOPF(dstdir, title, filelist, cover=None):
|
||||
"<item id=\"nav\" href=\"nav.xhtml\" ",
|
||||
"properties=\"nav\" media-type=\"application/xhtml+xml\"/>\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("<item id=\"cover\" href=\"Images/cover" + filename[1] + "\" media-type=\"" + mt +
|
||||
mt = 'image/jpeg'
|
||||
f.write("<item id=\"cover\" href=\"Images/cover.jpg" + "\" media-type=\"" + mt +
|
||||
"\" properties=\"cover-image\"/>\n")
|
||||
reflist = []
|
||||
for path in filelist:
|
||||
@@ -427,10 +423,9 @@ def buildOPF(dstdir, title, filelist, cover=None):
|
||||
"</container>"])
|
||||
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])
|
||||
|
||||
@@ -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.')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user