mirror of
https://github.com/ciromattia/kcc
synced 2025-12-20 05:01:55 +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\" ",
|
"<item id=\"nav\" href=\"nav.xhtml\" ",
|
||||||
"properties=\"nav\" media-type=\"application/xhtml+xml\"/>\n"])
|
"properties=\"nav\" media-type=\"application/xhtml+xml\"/>\n"])
|
||||||
if cover is not None:
|
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'
|
mt = 'image/jpeg'
|
||||||
f.write("<item id=\"cover\" href=\"Images/cover" + filename[1] + "\" media-type=\"" + mt +
|
f.write("<item id=\"cover\" href=\"Images/cover.jpg" + "\" media-type=\"" + mt +
|
||||||
"\" properties=\"cover-image\"/>\n")
|
"\" properties=\"cover-image\"/>\n")
|
||||||
reflist = []
|
reflist = []
|
||||||
for path in filelist:
|
for path in filelist:
|
||||||
@@ -427,10 +423,9 @@ def buildOPF(dstdir, title, filelist, cover=None):
|
|||||||
"</container>"])
|
"</container>"])
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def buildEPUB(path, chapternames, tomenumber, ischunked):
|
def buildEPUB(path, chapternames, tomenumber, ischunked, cover: image.Cover):
|
||||||
filelist = []
|
filelist = []
|
||||||
chapterlist = []
|
chapterlist = []
|
||||||
cover = None
|
|
||||||
os.mkdir(os.path.join(path, 'OEBPS', 'Text'))
|
os.mkdir(os.path.join(path, 'OEBPS', 'Text'))
|
||||||
f = open(os.path.join(path, 'OEBPS', 'Text', 'style.css'), 'w', encoding='UTF-8')
|
f = open(os.path.join(path, 'OEBPS', 'Text', 'style.css'), 'w', encoding='UTF-8')
|
||||||
f.writelines(["@page {\n",
|
f.writelines(["@page {\n",
|
||||||
@@ -508,18 +503,14 @@ def buildEPUB(path, chapternames, tomenumber, ischunked):
|
|||||||
"}\n"])
|
"}\n"])
|
||||||
f.close()
|
f.close()
|
||||||
build_html_start = perf_counter()
|
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')):
|
for dirpath, dirnames, filenames in os.walk(os.path.join(path, 'OEBPS', 'Images')):
|
||||||
chapter = False
|
chapter = False
|
||||||
dirnames, filenames = walkSort(dirnames, filenames)
|
dirnames, filenames = walkSort(dirnames, filenames)
|
||||||
for afile in filenames:
|
for afile in filenames:
|
||||||
if cover is None:
|
if afile == 'cover.jpg':
|
||||||
try:
|
continue
|
||||||
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 not chapter:
|
if not chapter:
|
||||||
chapterlist.append((dirpath.replace('Images', 'Text'), afile))
|
chapterlist.append((dirpath.replace('Images', 'Text'), afile))
|
||||||
chapter = True
|
chapter = True
|
||||||
@@ -801,6 +792,7 @@ def getPanelViewSize(deviceres, size):
|
|||||||
def sanitizeTree(filetree):
|
def sanitizeTree(filetree):
|
||||||
chapterNames = {}
|
chapterNames = {}
|
||||||
page = 1
|
page = 1
|
||||||
|
cover_path = None
|
||||||
for root, dirs, files in os.walk(filetree):
|
for root, dirs, files in os.walk(filetree):
|
||||||
dirs.sort(key=OS_SORT_KEY)
|
dirs.sort(key=OS_SORT_KEY)
|
||||||
files.sort(key=OS_SORT_KEY)
|
files.sort(key=OS_SORT_KEY)
|
||||||
@@ -815,6 +807,8 @@ def sanitizeTree(filetree):
|
|||||||
key = os.path.join(root, name)
|
key = os.path.join(root, name)
|
||||||
if key != newKey:
|
if key != newKey:
|
||||||
os.replace(key, newKey)
|
os.replace(key, newKey)
|
||||||
|
if not cover_path:
|
||||||
|
cover_path = newKey
|
||||||
for i, name in enumerate(dirs):
|
for i, name in enumerate(dirs):
|
||||||
tmpName = name
|
tmpName = name
|
||||||
slugified = slugify(name)
|
slugified = slugify(name)
|
||||||
@@ -826,7 +820,7 @@ def sanitizeTree(filetree):
|
|||||||
if key != newKey:
|
if key != newKey:
|
||||||
os.replace(key, newKey)
|
os.replace(key, newKey)
|
||||||
dirs[i] = newKey
|
dirs[i] = newKey
|
||||||
return chapterNames
|
return chapterNames, cover_path
|
||||||
|
|
||||||
|
|
||||||
def flattenTree(filetree):
|
def flattenTree(filetree):
|
||||||
@@ -1242,7 +1236,9 @@ def makeBook(source, qtgui=None):
|
|||||||
print("Checking images...")
|
print("Checking images...")
|
||||||
getComicInfo(os.path.join(path, "OEBPS", "Images"), source)
|
getComicInfo(os.path.join(path, "OEBPS", "Images"), source)
|
||||||
detectSuboptimalProcessing(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:
|
if options.webtoon:
|
||||||
y = image.ProfileData.Profiles[options.profile][1][1]
|
y = image.ProfileData.Profiles[options.profile][1][1]
|
||||||
comic2panel.main(['-y ' + str(y), '-i', '-m', path], qtgui)
|
comic2panel.main(['-y ' + str(y), '-i', '-m', path], qtgui)
|
||||||
@@ -1288,10 +1284,10 @@ def makeBook(source, qtgui=None):
|
|||||||
else:
|
else:
|
||||||
print("Creating EPUB file...")
|
print("Creating EPUB file...")
|
||||||
if len(tomes) > 1:
|
if len(tomes) > 1:
|
||||||
buildEPUB(tome, chapterNames, tomeNumber, True)
|
buildEPUB(tome, chapterNames, tomeNumber, True, cover)
|
||||||
filepath.append(getOutputFilename(source, options.output, '.epub', ' ' + str(tomeNumber)))
|
filepath.append(getOutputFilename(source, options.output, '.epub', ' ' + str(tomeNumber)))
|
||||||
else:
|
else:
|
||||||
buildEPUB(tome, chapterNames, tomeNumber, False)
|
buildEPUB(tome, chapterNames, tomeNumber, False, cover)
|
||||||
filepath.append(getOutputFilename(source, options.output, '.epub', ''))
|
filepath.append(getOutputFilename(source, options.output, '.epub', ''))
|
||||||
makeZIP(tome + '_comic', tome, True)
|
makeZIP(tome + '_comic', tome, True)
|
||||||
copyfile(tome + '_comic.zip', filepath[-1])
|
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)
|
self.image = crop_empty_inter_panel(self.image, direction, background_color=self.fill)
|
||||||
|
|
||||||
class Cover:
|
class Cover:
|
||||||
def __init__(self, source, target, opt, tomeid):
|
def __init__(self, source, opt):
|
||||||
self.options = opt
|
self.options = opt
|
||||||
self.source = source
|
self.source = source
|
||||||
self.target = target
|
|
||||||
if tomeid == 0:
|
|
||||||
self.tomeid = 1
|
|
||||||
else:
|
|
||||||
self.tomeid = tomeid
|
|
||||||
self.image = Image.open(source)
|
self.image = Image.open(source)
|
||||||
# backwards compatibility for Pillow >9.1.0
|
# backwards compatibility for Pillow >9.1.0
|
||||||
if not hasattr(Image, 'Resampling'):
|
if not hasattr(Image, 'Resampling'):
|
||||||
@@ -433,6 +428,15 @@ class Cover:
|
|||||||
self.image = ImageOps.autocontrast(self.image)
|
self.image = ImageOps.autocontrast(self.image)
|
||||||
if not self.options.forcecolor:
|
if not self.options.forcecolor:
|
||||||
self.image = self.image.convert('L')
|
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
|
w, h = self.image.size
|
||||||
if w / h > 2:
|
if w / h > 2:
|
||||||
if self.options.righttoleft:
|
if self.options.righttoleft:
|
||||||
@@ -444,12 +448,14 @@ class Cover:
|
|||||||
self.image = self.image.crop((0, 0, w/2 - w * 0.03, h))
|
self.image = self.image.crop((0, 0, w/2 - w * 0.03, h))
|
||||||
else:
|
else:
|
||||||
self.image = self.image.crop((w/2 + w * 0.03, 0, w, h))
|
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:
|
try:
|
||||||
self.image.save(self.target, "JPEG", optimize=1, quality=85)
|
self.image.save(target, "JPEG", optimize=1, quality=85)
|
||||||
except IOError:
|
except IOError:
|
||||||
raise RuntimeError('Failed to save cover.')
|
raise RuntimeError('Failed to save cover.')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user