mirror of
https://github.com/ciromattia/kcc
synced 2026-07-01 18:45:26 +00:00
Code cleaning
This commit is contained in:
+11
-37
@@ -270,8 +270,7 @@ def applyImgOptimization(img, isSplit=False, toRight=False):
|
|||||||
img.cropWhiteSpace(10.0)
|
img.cropWhiteSpace(10.0)
|
||||||
if options.cutpagenumbers:
|
if options.cutpagenumbers:
|
||||||
img.cutPageNumber()
|
img.cutPageNumber()
|
||||||
img.resizeImage(options.upscale, options.stretch, options.black_borders, options.fakepanelviewlandscape, isSplit,
|
img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode)
|
||||||
toRight, options.landscapemode)
|
|
||||||
img.optimizeImage(options.gamma)
|
img.optimizeImage(options.gamma)
|
||||||
if not options.notquantize:
|
if not options.notquantize:
|
||||||
img.quantizeImage()
|
img.quantizeImage()
|
||||||
@@ -313,42 +312,19 @@ def dirImgProcess(path):
|
|||||||
if facing == "right":
|
if facing == "right":
|
||||||
splitCount += 1
|
splitCount += 1
|
||||||
facing = "left"
|
facing = "left"
|
||||||
if options.fakepanelview or options.fakepanelviewlandscape:
|
img0 = image.ComicPage(split[0], options.profile)
|
||||||
img0 = image.ComicPage(split[0], options.profile)
|
applyImgOptimization(img0, True, toRight1)
|
||||||
img1 = image.ComicPage(split[1], options.profile)
|
img0.saveToDir(dirpath, options.notquantize)
|
||||||
splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft,
|
img1 = image.ComicPage(split[1], options.profile)
|
||||||
options.fakepanelviewlandscape)
|
applyImgOptimization(img1, True, toRight2)
|
||||||
splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft,
|
img1.saveToDir(dirpath, options.notquantize)
|
||||||
options.fakepanelviewlandscape)
|
|
||||||
for img in splitA:
|
|
||||||
tempImg = image.ComicPage(img, options.profile)
|
|
||||||
applyImgOptimization(tempImg)
|
|
||||||
tempImg.saveToDir(dirpath, options.notquantize)
|
|
||||||
for img in splitB:
|
|
||||||
tempImg = image.ComicPage(img, options.profile)
|
|
||||||
applyImgOptimization(tempImg)
|
|
||||||
tempImg.saveToDir(dirpath, options.notquantize)
|
|
||||||
else:
|
|
||||||
img0 = image.ComicPage(split[0], options.profile)
|
|
||||||
applyImgOptimization(img0, True, toRight1)
|
|
||||||
img0.saveToDir(dirpath, options.notquantize)
|
|
||||||
img1 = image.ComicPage(split[1], options.profile)
|
|
||||||
applyImgOptimization(img1, True, toRight2)
|
|
||||||
img1.saveToDir(dirpath, options.notquantize)
|
|
||||||
else:
|
else:
|
||||||
if facing == "right":
|
if facing == "right":
|
||||||
facing = "left"
|
facing = "left"
|
||||||
else:
|
else:
|
||||||
facing = "right"
|
facing = "right"
|
||||||
if options.fakepanelview or options.fakepanelviewlandscape:
|
applyImgOptimization(img)
|
||||||
split = img.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape)
|
img.saveToDir(dirpath, options.notquantize)
|
||||||
for img in split:
|
|
||||||
tempImg = image.ComicPage(img, options.profile)
|
|
||||||
applyImgOptimization(tempImg)
|
|
||||||
tempImg.saveToDir(dirpath, options.notquantize)
|
|
||||||
else:
|
|
||||||
applyImgOptimization(img)
|
|
||||||
img.saveToDir(dirpath, options.notquantize)
|
|
||||||
|
|
||||||
|
|
||||||
def genEpubStruct(path):
|
def genEpubStruct(path):
|
||||||
@@ -464,10 +440,8 @@ def main(argv=None):
|
|||||||
help="Comic title [Default=filename]")
|
help="Comic title [Default=filename]")
|
||||||
parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False,
|
parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False,
|
||||||
help="Manga style (Right-to-left reading and splitting) [Default=False]")
|
help="Manga style (Right-to-left reading and splitting) [Default=False]")
|
||||||
parser.add_option("--panelview", action="store_true", dest="fakepanelview", default=False,
|
parser.add_option("--panelview", action="store_true", dest="panelview", default=False,
|
||||||
help="Emulate Panel View feature (For Kindle 4 NT or older) [Default=False]")
|
help="Add Panel View support (For Kindle Classic or older) [Default=False]")
|
||||||
parser.add_option("--panelviewlandscape", action="store_true", dest="fakepanelviewlandscape", default=False,
|
|
||||||
help="Emulate Panel View feature - Landscape mode (For Kindle 4 NT or older) [Default=False]")
|
|
||||||
parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True,
|
parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True,
|
||||||
help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]")
|
help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]")
|
||||||
parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False,
|
parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False,
|
||||||
|
|||||||
+58
-111
@@ -141,15 +141,13 @@ class ComicPage:
|
|||||||
palImg.putpalette(self.palette)
|
palImg.putpalette(self.palette)
|
||||||
self.image = self.image.quantize(palette=palImg)
|
self.image = self.image.quantize(palette=palImg)
|
||||||
|
|
||||||
def resizeImage(self, upscale=False, stretch=False, black_borders=False, fakePanelViewLandscape=False,
|
def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False,
|
||||||
isSplit=False, toRight=False, landscapeMode=False):
|
landscapeMode=False):
|
||||||
method = Image.ANTIALIAS
|
method = Image.ANTIALIAS
|
||||||
if black_borders:
|
if black_borders:
|
||||||
fill = 'black'
|
fill = 'black'
|
||||||
else:
|
else:
|
||||||
fill = 'white'
|
fill = 'white'
|
||||||
if fakePanelViewLandscape:
|
|
||||||
self.image = self.image.rotate(90)
|
|
||||||
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
|
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
|
||||||
if not upscale:
|
if not upscale:
|
||||||
if isSplit and landscapeMode:
|
if isSplit and landscapeMode:
|
||||||
@@ -223,80 +221,6 @@ class ComicPage:
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def splitPageFakePanelView(self, targetdir, righttoleft=False, fakePanelWiewLandscape=False):
|
|
||||||
width, height = self.image.size
|
|
||||||
if fakePanelWiewLandscape:
|
|
||||||
topbox = (0, 0, width, ((height / 2) + (height / 9)))
|
|
||||||
bottombox = (0, ((height / 2) - (height / 9)), width, height)
|
|
||||||
filename = os.path.splitext(os.path.basename(self.origFileName))
|
|
||||||
file1 = targetdir + '/' + filename[0] + '-1' + filename[1]
|
|
||||||
file2 = targetdir + '/' + filename[0] + '-2' + filename[1]
|
|
||||||
try:
|
|
||||||
page1 = self.image.crop(topbox)
|
|
||||||
page2 = self.image.crop(bottombox)
|
|
||||||
page1.save(file1)
|
|
||||||
page2.save(file2)
|
|
||||||
os.remove(self.origFileName)
|
|
||||||
except IOError as e:
|
|
||||||
raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
|
|
||||||
return file1, file2
|
|
||||||
else:
|
|
||||||
topleftbox = (0, 0, ((width / 2) + (width / 9)), ((height / 2) + (height / 9)))
|
|
||||||
toprightbox = ((width / 2) - (width / 9)), 0, width, ((height / 2) + (height / 9))
|
|
||||||
bottomleftbox = (0, ((height / 2) - (height / 9)), ((width / 2) + (width / 9)), height)
|
|
||||||
bototmrightbox = (((width / 2) - (width / 9)), ((height / 2) - (height / 9)), width, height)
|
|
||||||
filename = os.path.splitext(os.path.basename(self.origFileName))
|
|
||||||
file0 = targetdir + '/' + filename[0] + '-0' + filename[1]
|
|
||||||
file1 = targetdir + '/' + filename[0] + '-1' + filename[1]
|
|
||||||
file2 = targetdir + '/' + filename[0] + '-2' + filename[1]
|
|
||||||
file3 = targetdir + '/' + filename[0] + '-3' + filename[1]
|
|
||||||
file4 = targetdir + '/' + filename[0] + '-4' + filename[1]
|
|
||||||
try:
|
|
||||||
if righttoleft:
|
|
||||||
page0 = self.image
|
|
||||||
page1 = self.image.crop(toprightbox)
|
|
||||||
page2 = self.image.crop(topleftbox)
|
|
||||||
page3 = self.image.crop(bototmrightbox)
|
|
||||||
page4 = self.image.crop(bottomleftbox)
|
|
||||||
else:
|
|
||||||
page0 = self.image
|
|
||||||
page1 = self.image.crop(topleftbox)
|
|
||||||
page2 = self.image.crop(toprightbox)
|
|
||||||
page3 = self.image.crop(bottomleftbox)
|
|
||||||
page4 = self.image.crop(bototmrightbox)
|
|
||||||
page0.save(file0)
|
|
||||||
page1.save(file1)
|
|
||||||
page2.save(file2)
|
|
||||||
page3.save(file3)
|
|
||||||
page4.save(file4)
|
|
||||||
os.remove(self.origFileName)
|
|
||||||
except IOError as e:
|
|
||||||
raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
|
|
||||||
return file0, file1, file2, file3, file4
|
|
||||||
|
|
||||||
# def frameImage(self):
|
|
||||||
# foreground = tuple(self.palette[:3])
|
|
||||||
# background = tuple(self.palette[-3:])
|
|
||||||
# widthDev, heightDev = self.size
|
|
||||||
# widthImg, heightImg = self.image.size
|
|
||||||
# pastePt = (
|
|
||||||
# max(0, (widthDev - widthImg) / 2),
|
|
||||||
# max(0, (heightDev - heightImg) / 2)
|
|
||||||
# )
|
|
||||||
# corner1 = (
|
|
||||||
# pastePt[0] - 1,
|
|
||||||
# pastePt[1] - 1
|
|
||||||
# )
|
|
||||||
# corner2 = (
|
|
||||||
# pastePt[0] + widthImg + 1,
|
|
||||||
# pastePt[1] + heightImg + 1
|
|
||||||
# )
|
|
||||||
# imageBg = Image.new(self.image.mode, self.size, background)
|
|
||||||
# imageBg.paste(self.image, pastePt)
|
|
||||||
# draw = ImageDraw.Draw(imageBg)
|
|
||||||
# draw.rectangle([corner1, corner2], outline=foreground)
|
|
||||||
# self.image = imageBg
|
|
||||||
|
|
||||||
def cutPageNumber(self):
|
def cutPageNumber(self):
|
||||||
widthImg, heightImg = self.image.size
|
widthImg, heightImg = self.image.size
|
||||||
delta = 2
|
delta = 2
|
||||||
@@ -390,36 +314,59 @@ class ComicPage:
|
|||||||
return self.image
|
return self.image
|
||||||
|
|
||||||
# def addProgressbar(self, file_number, files_totalnumber, size, howoften):
|
# def addProgressbar(self, file_number, files_totalnumber, size, howoften):
|
||||||
# if file_number // howoften != float(file_number) / howoften:
|
# if file_number // howoften != float(file_number) / howoften:
|
||||||
# return self.image
|
# return self.image
|
||||||
# white = (255, 255, 255)
|
# white = (255, 255, 255)
|
||||||
# black = (0, 0, 0)
|
# black = (0, 0, 0)
|
||||||
# widthDev, heightDev = size
|
# widthDev, heightDev = size
|
||||||
# widthImg, heightImg = self.image.size
|
# widthImg, heightImg = self.image.size
|
||||||
# pastePt = (
|
# pastePt = (
|
||||||
# max(0, (widthDev - widthImg) / 2),
|
# max(0, (widthDev - widthImg) / 2),
|
||||||
# max(0, (heightDev - heightImg) / 2)
|
# max(0, (heightDev - heightImg) / 2)
|
||||||
# )
|
# )
|
||||||
# imageBg = Image.new('RGB', size, white)
|
# imageBg = Image.new('RGB', size, white)
|
||||||
# imageBg.paste(self.image, pastePt)
|
# imageBg.paste(self.image, pastePt)
|
||||||
# self.image = imageBg
|
# self.image = imageBg
|
||||||
# widthImg, heightImg = self.image.size
|
# widthImg, heightImg = self.image.size
|
||||||
# draw = ImageDraw.Draw(self.image)
|
# draw = ImageDraw.Draw(self.image)
|
||||||
# Black rectangle
|
# #Black rectangle
|
||||||
# draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black)
|
# draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black)
|
||||||
# White rectangle
|
# #White rectangle
|
||||||
# draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)],
|
# draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)],
|
||||||
# outline=black, fill=white)
|
# outline=black, fill=white)
|
||||||
# Making notches
|
# #Making notches
|
||||||
# for i in range(1, 10):
|
# for i in range(1, 10):
|
||||||
# if i <= (10 * file_number / files_totalnumber):
|
# if i <= (10 * file_number / files_totalnumber):
|
||||||
# notch_colour = white # White
|
# notch_colour = white # White
|
||||||
# else:
|
# else:
|
||||||
# notch_colour = black # Black
|
# notch_colour = black # Black
|
||||||
# draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)],
|
# draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)],
|
||||||
# fill=notch_colour)
|
# fill=notch_colour)
|
||||||
# The 50%
|
# #The 50%
|
||||||
# if i == 5:
|
# if i == 5:
|
||||||
# draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)],
|
# draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)],
|
||||||
# outline=black, fill=notch_colour)
|
# outline=black, fill=notch_colour)
|
||||||
# return self.image
|
# return self.image
|
||||||
|
#
|
||||||
|
# def frameImage(self):
|
||||||
|
# foreground = tuple(self.palette[:3])
|
||||||
|
# background = tuple(self.palette[-3:])
|
||||||
|
# widthDev, heightDev = self.size
|
||||||
|
# widthImg, heightImg = self.image.size
|
||||||
|
# pastePt = (
|
||||||
|
# max(0, (widthDev - widthImg) / 2),
|
||||||
|
# max(0, (heightDev - heightImg) / 2)
|
||||||
|
# )
|
||||||
|
# corner1 = (
|
||||||
|
# pastePt[0] - 1,
|
||||||
|
# pastePt[1] - 1
|
||||||
|
# )
|
||||||
|
# corner2 = (
|
||||||
|
# pastePt[0] + widthImg + 1,
|
||||||
|
# pastePt[1] + heightImg + 1
|
||||||
|
# )
|
||||||
|
# imageBg = Image.new(self.image.mode, self.size, background)
|
||||||
|
# imageBg.paste(self.image, pastePt)
|
||||||
|
# draw = ImageDraw.Draw(imageBg)
|
||||||
|
# draw.rectangle([corner1, corner2], outline=foreground)
|
||||||
|
# self.image = imageBg
|
||||||
|
|||||||
Reference in New Issue
Block a user