1
0
mirror of https://github.com/ciromattia/kcc synced 2026-01-05 04:49:36 +00:00

Code cleanup

This commit is contained in:
Paweł Jastrzębski
2013-09-28 19:57:49 +02:00
parent 27e01657b1
commit 97b44a89d4
5 changed files with 63 additions and 57 deletions

View File

@@ -138,13 +138,12 @@ The app relies and includes the following scripts/binaries:
## SAMPLE FILES CREATED BY KCC
* [Kindle Paperwhite](http://kcc.vulturis.eu/Samples/Ubunchu!-KPW.mobi)
* [Kindle](http://kcc.vulturis.eu/Samples/Ubunchu!-K345.mobi)
* [Kindle DX](http://kcc.vulturis.eu/Samples/Ubunchu!-KDX.mobi)
* [Kindle Fire](http://kcc.vulturis.eu/Samples/Ubunchu!-KF.mobi)
* [Kindle Fire HD](http://kcc.vulturis.eu/Samples/Ubunchu!-KFHD.mobi)
* [Kindle Fire HD 8.9"](http://kcc.vulturis.eu/Samples/Ubunchu!-KFHD8.mobi)
* [Kindle Fire HDX](http://kcc.vulturis.eu/Samples/Ubunchu!-KFHDX.mobi)
* [Kindle Fire HDX 8.9"](http://kcc.vulturis.eu/Samples/Ubunchu!-KFHDX8.mobi)
* [Kindle DX](http://kcc.vulturis.eu/Samples/Ubunchu!-KDX.mobi)
* [Kindle DXG](http://kcc.vulturis.eu/Samples/Ubunchu!-KDXG.mobi)
## CHANGELOG
####1.00

View File

@@ -99,6 +99,10 @@ class WorkerThread(QtCore.QThread):
def __init__(self, parent):
QtCore.QThread.__init__(self)
self.parent = parent
self.conversionAlive = False
self.errors = False
self.kindlegenErrorCode = 0
self.kindlegenError = None
def __del__(self):
self.wait()
@@ -610,6 +614,7 @@ class Ui_KCC(object):
self.versionCheck = VersionThread(self)
self.conversionAlive = False
self.needClean = True
self.GammaValue = 1.0
self.addMessage('<b>Welcome!</b>', 'info')
self.addMessage('<b>Remember:</b> All options have additional informations in tooltips.', 'info')

View File

@@ -287,23 +287,22 @@ def getImageFileName(imgfile):
return filename
def applyImgOptimization(img, options, overrideQuality=5):
img.getImageFill(options.webtoon)
if not options.webtoon:
def applyImgOptimization(img, opt, overrideQuality=5):
img.getImageFill(opt.webtoon)
if not opt.webtoon:
img.cropWhiteSpace(10.0)
if options.cutpagenumbers and not options.webtoon:
if opt.cutpagenumbers and not opt.webtoon:
img.cutPageNumber()
img.optimizeImage(options.gamma)
img.optimizeImage(opt.gamma)
if overrideQuality != 5:
img.resizeImage(options.upscale, options.stretch, options.bordersColor, overrideQuality)
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, overrideQuality)
else:
img.resizeImage(options.upscale, options.stretch, options.bordersColor, options.quality)
if options.forcepng and not options.forcecolor:
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, opt.quality)
if opt.forcepng and not opt.forcecolor:
img.quantizeImage()
def dirImgProcess(path):
global options
work = []
pagenumber = 0
pagenumbermodifier = 0
@@ -349,9 +348,9 @@ def dirImgProcess(path):
raise UserWarning("Source directory is empty.")
def fileImgProcess_init(queue, options):
def fileImgProcess_init(queue, opt):
fileImgProcess.queue = queue
fileImgProcess.options = options
fileImgProcess.options = opt
# noinspection PyUnresolvedReferences
@@ -359,54 +358,53 @@ def fileImgProcess(work):
afile = work[0]
dirpath = work[1]
pagenumber = work[2]
options = fileImgProcess.options
opt = fileImgProcess.options
output = None
if options.verbose:
print "Optimizing " + afile + " for " + options.profile
if opt.verbose:
print "Optimizing " + afile + " for " + opt.profile
else:
print ".",
fileImgProcess.queue.put(".")
img = image.ComicPage(os.path.join(dirpath, afile), options.profileData)
if options.quality == 2:
img = image.ComicPage(os.path.join(dirpath, afile), opt.profileData)
if opt.quality == 2:
wipe = False
else:
wipe = True
if options.nosplitrotate:
if opt.nosplitrotate:
split = None
else:
split = img.splitPage(dirpath, options.righttoleft, options.rotate)
split = img.splitPage(dirpath, opt.righttoleft, opt.rotate)
if split is not None:
if options.verbose:
if opt.verbose:
print "Splitted " + afile
output = pagenumber
img0 = image.ComicPage(split[0], options.profileData)
applyImgOptimization(img0, options)
img0.saveToDir(dirpath, options.forcepng, options.forcecolor, wipe)
img1 = image.ComicPage(split[1], options.profileData)
applyImgOptimization(img1, options)
img1.saveToDir(dirpath, options.forcepng, options.forcecolor, wipe)
if options.quality == 2:
img3 = image.ComicPage(split[0], options.profileData)
applyImgOptimization(img3, options, 0)
img3.saveToDir(dirpath, options.forcepng, options.forcecolor, True)
img4 = image.ComicPage(split[1], options.profileData)
applyImgOptimization(img4, options, 0)
img4.saveToDir(dirpath, options.forcepng, options.forcecolor, True)
img0 = image.ComicPage(split[0], opt.profileData)
applyImgOptimization(img0, opt)
img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe)
img1 = image.ComicPage(split[1], opt.profileData)
applyImgOptimization(img1, opt)
img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe)
if opt.quality == 2:
img3 = image.ComicPage(split[0], opt.profileData)
applyImgOptimization(img3, opt, 0)
img3.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True)
img4 = image.ComicPage(split[1], opt.profileData)
applyImgOptimization(img4, opt, 0)
img4.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True)
else:
applyImgOptimization(img, options)
img.saveToDir(dirpath, options.forcepng, options.forcecolor, wipe)
if options.quality == 2:
img2 = image.ComicPage(os.path.join(dirpath, afile), options.profileData)
applyImgOptimization(img, opt)
img.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe)
if opt.quality == 2:
img2 = image.ComicPage(os.path.join(dirpath, afile), opt.profileData)
if img.rotated:
img2.image = img2.image.rotate(90)
img2.rotated = True
applyImgOptimization(img2, options, 0)
img2.saveToDir(dirpath, options.forcepng, options.forcecolor, True)
applyImgOptimization(img2, opt, 0)
img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True)
return output
def genEpubStruct(path):
global options
filelist = []
chapterlist = []
cover = None
@@ -584,6 +582,7 @@ def getWorkFolder(afile):
def slugify(value):
# Normalizes string, converts to lowercase, removes non-alpha characters and converts spaces to hyphens.
import unicodedata
#noinspection PyArgumentList
value = unicodedata.normalize('NFKD', unicode(value, 'latin1')).encode('ascii', 'ignore')
value = re.sub('[^\w\s\.-]', '', value).strip().lower()
value = re.sub('[-\.\s]+', '-', value)
@@ -788,7 +787,7 @@ def Usage():
def main(argv=None, qtGUI=None):
global parser, options, epub_path, GUI
global parser, options, GUI
parser = OptionParser(usage="Usage: %prog [options] comic_file|comic_folder", add_help_option=False)
mainOptions = OptionGroup(parser, "MAIN")
processingOptions = OptionGroup(parser, "PROCESSING")

View File

@@ -54,9 +54,9 @@ def getImageFileName(imgfile):
return filename
def sanitizePanelSize(panel, options):
def sanitizePanelSize(panel, opt):
newPanels = []
if panel[2] > 8 * options.height:
if panel[2] > 8 * opt.height:
diff = (panel[2] / 8)
newPanels.append([panel[0], panel[1] - diff*7, diff])
newPanels.append([panel[1] - diff*7, panel[1] - diff*6, diff])
@@ -66,13 +66,13 @@ def sanitizePanelSize(panel, options):
newPanels.append([panel[1] - diff*3, panel[1] - diff*2, diff])
newPanels.append([panel[1] - diff*2, panel[1] - diff, diff])
newPanels.append([panel[1] - diff, panel[1], diff])
elif panel[2] > 4 * options.height:
elif panel[2] > 4 * opt.height:
diff = (panel[2] / 4)
newPanels.append([panel[0], panel[1] - diff*3, diff])
newPanels.append([panel[1] - diff*3, panel[1] - diff*2, diff])
newPanels.append([panel[1] - diff*2, panel[1] - diff, diff])
newPanels.append([panel[1] - diff, panel[1], diff])
elif panel[2] > 2 * options.height:
elif panel[2] > 2 * opt.height:
newPanels.append([panel[0], panel[1] - (panel[2] / 2), (panel[2] / 2)])
newPanels.append([panel[1] - (panel[2] / 2), panel[1], (panel[2] / 2)])
else:
@@ -80,17 +80,17 @@ def sanitizePanelSize(panel, options):
return newPanels
def splitImage_init(queue, options):
def splitImage_init(queue, opt):
splitImage.queue = queue
splitImage.options = options
splitImage.options = opt
# noinspection PyUnresolvedReferences
def splitImage(work):
path = work[0]
name = work[1]
options = splitImage.options
# Harcoded options
opt = splitImage.options
# Harcoded opttions
threshold = 1.0
delta = 15
print ".",
@@ -115,8 +115,8 @@ def splitImage(work):
image = Image.open(filePath)
image = image.convert('RGB')
widthImg, heightImg = image.size
if heightImg > options.height:
if options.debug:
if heightImg > opt.height:
if opt.debug:
from PIL import ImageDraw
debugImage = Image.open(filePath)
draw = ImageDraw.Draw(debugImage)
@@ -138,23 +138,23 @@ def splitImage(work):
if y1 + delta >= heightImg:
y1 = heightImg - 1
y2Temp = y1
if options.debug:
if opt.debug:
draw.line([(0, y1Temp), (widthImg, y1Temp)], fill=(0, 255, 0))
draw.line([(0, y2Temp), (widthImg, y2Temp)], fill=(255, 0, 0))
panelHeight = y2Temp - y1Temp
if panelHeight > delta:
# Panels that can't be cut nicely will be forcefully splitted
panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight], options)
panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight], opt)
for panel in panelsCleaned:
panels.append(panel)
if options.debug:
if opt.debug:
# noinspection PyUnboundLocalVariable
debugImage.save(os.path.join(path, fileExpanded[0] + '-debug.png'), 'PNG')
# Create virtual pages
pages = []
currentPage = []
pageLeft = options.height
pageLeft = opt.height
panelNumber = 0
for panel in panels:
if pageLeft - panel[2] > 0:
@@ -164,7 +164,7 @@ def splitImage(work):
else:
if len(currentPage) > 0:
pages.append(currentPage)
pageLeft = options.height - panel[2]
pageLeft = opt.height - panel[2]
currentPage = [panelNumber]
panelNumber += 1
if len(currentPage) > 0:

View File

@@ -30,6 +30,9 @@ except ImportError:
class ProfileData:
def __init__(self):
pass
Palette4 = [
0x00, 0x00, 0x00,
0x55, 0x55, 0x55,