mirror of
https://github.com/ciromattia/kcc
synced 2025-12-24 07:01:51 +00:00
Margins color detection now handles every file
This commit is contained in:
@@ -145,10 +145,13 @@
|
||||
<enum>Qt::NoFocus</enum>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string><html><head/><body><p><span style=" font-size:12pt;">Fill space around images with black color.</span></p></body></html></string>
|
||||
<string><html><head/><body><p><span style=" font-weight:600; text-decoration: underline;">Unchecked - Autodetection<br/></span>Color of margins fill will be detected automatically.</p><p><span style=" font-weight:600; text-decoration: underline;">Indeterminate - White<br/></span>Margins will be filled with white color.</p><p><span style=" font-weight:600; text-decoration: underline;">Checked - Black<br/></span>Margins will be filled with black color.</p></body></html></string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Black borders</string>
|
||||
<string>W/B margins</string>
|
||||
</property>
|
||||
<property name="tristate">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
7
KCC.ui
7
KCC.ui
@@ -123,10 +123,13 @@
|
||||
<enum>Qt::NoFocus</enum>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Fill space around images with black color.</string>
|
||||
<string><html><head/><body><p><span style=" font-weight:600; text-decoration: underline;">Unchecked - Autodetection<br/></span>Color of margins fill will be detected automatically.</p><p><span style=" font-weight:600; text-decoration: underline;">Indeterminate - White<br/></span>Margins will be filled with white color.</p><p><span style=" font-weight:600; text-decoration: underline;">Checked - Black<br/></span>Margins will be filled with black color.</p></body></html></string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Black borders</string>
|
||||
<string>W/B margins</string>
|
||||
</property>
|
||||
<property name="tristate">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
@@ -129,12 +129,14 @@ class WorkerThread(QtCore.QThread):
|
||||
argv.append("--noprocessing")
|
||||
if GUI.NoRotateBox.isChecked():
|
||||
argv.append("--nosplitrotate")
|
||||
if GUI.BorderBox.isChecked():
|
||||
argv.append("--blackborders")
|
||||
if GUI.UpscaleBox.checkState() == 1:
|
||||
argv.append("--stretch")
|
||||
elif GUI.UpscaleBox.checkState() == 2:
|
||||
argv.append("--upscale")
|
||||
if GUI.BorderBox.checkState() == 1:
|
||||
argv.append("--whiteborders")
|
||||
elif GUI.BorderBox.checkState() == 2:
|
||||
argv.append("--blackborders")
|
||||
if GUI.NoDitheringBox.isChecked():
|
||||
argv.append("--forcepng")
|
||||
if GUI.WebtoonBox.isChecked():
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
# Form implementation generated from reading ui file 'KCC.ui'
|
||||
#
|
||||
# Created: Sat Sep 14 10:28:36 2013
|
||||
# Created: Sun Sep 15 16:18:37 2013
|
||||
# by: PyQt4 UI code generator 4.10.3
|
||||
#
|
||||
# WARNING! All changes made in this file will be lost!
|
||||
@@ -68,6 +68,7 @@ class Ui_KCC(object):
|
||||
self.gridLayout.addWidget(self.NoDitheringBox, 3, 2, 1, 1)
|
||||
self.BorderBox = QtGui.QCheckBox(self.OptionsAdvanced)
|
||||
self.BorderBox.setFocusPolicy(QtCore.Qt.NoFocus)
|
||||
self.BorderBox.setTristate(True)
|
||||
self.BorderBox.setObjectName(_fromUtf8("BorderBox"))
|
||||
self.gridLayout.addWidget(self.BorderBox, 3, 0, 1, 1)
|
||||
self.NoRotateBox = QtGui.QCheckBox(self.OptionsAdvanced)
|
||||
@@ -270,8 +271,8 @@ class Ui_KCC(object):
|
||||
self.WebtoonBox.setText(_translate("KCC", "Webtoon mode", None))
|
||||
self.NoDitheringBox.setToolTip(_translate("KCC", "<html><head/><body><p>Create PNG files instead JPEG.<br/><span style=\" font-weight:600;\">Only for non-Kindle devices!</span></p></body></html>", None))
|
||||
self.NoDitheringBox.setText(_translate("KCC", "PNG output", None))
|
||||
self.BorderBox.setToolTip(_translate("KCC", "Fill space around images with black color.", None))
|
||||
self.BorderBox.setText(_translate("KCC", "Black borders", None))
|
||||
self.BorderBox.setToolTip(_translate("KCC", "<html><head/><body><p><span style=\" font-weight:600; text-decoration: underline;\">Unchecked - Autodetection<br/></span>Color of margins fill will be detected automatically.</p><p><span style=\" font-weight:600; text-decoration: underline;\">Indeterminate - White<br/></span>Margins will be filled with white color.</p><p><span style=\" font-weight:600; text-decoration: underline;\">Checked - Black<br/></span>Margins will be filled with black color.</p></body></html>", None))
|
||||
self.BorderBox.setText(_translate("KCC", "W/B margins", None))
|
||||
self.NoRotateBox.setToolTip(_translate("KCC", "<html><head/><body><p>Disable splitting and rotation.</p></body></html>", None))
|
||||
self.NoRotateBox.setText(_translate("KCC", "No split/rotate", None))
|
||||
self.DeviceBox.setToolTip(_translate("KCC", "Target device.", None))
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
# Form implementation generated from reading ui file 'KCC-OSX.ui'
|
||||
#
|
||||
# Created: Sat Sep 14 10:28:47 2013
|
||||
# Created: Sun Sep 15 16:18:48 2013
|
||||
# by: PyQt4 UI code generator 4.10.3
|
||||
#
|
||||
# WARNING! All changes made in this file will be lost!
|
||||
@@ -82,6 +82,7 @@ class Ui_KCC(object):
|
||||
font.setPointSize(11)
|
||||
self.BorderBox.setFont(font)
|
||||
self.BorderBox.setFocusPolicy(QtCore.Qt.NoFocus)
|
||||
self.BorderBox.setTristate(True)
|
||||
self.BorderBox.setObjectName(_fromUtf8("BorderBox"))
|
||||
self.gridLayout.addWidget(self.BorderBox, 3, 0, 1, 1)
|
||||
self.NoRotateBox = QtGui.QCheckBox(self.OptionsAdvanced)
|
||||
@@ -328,8 +329,8 @@ class Ui_KCC(object):
|
||||
self.WebtoonBox.setText(_translate("KCC", "Webtoon mode", None))
|
||||
self.NoDitheringBox.setToolTip(_translate("KCC", "<html><head/><body><p><span style=\" font-size:12pt;\">Create PNG files instead JPEG.<br/></span><span style=\" font-size:12pt; font-weight:600;\">Only for non-Kindle devices!</span></p></body></html>", None))
|
||||
self.NoDitheringBox.setText(_translate("KCC", "PNG output", None))
|
||||
self.BorderBox.setToolTip(_translate("KCC", "<html><head/><body><p><span style=\" font-size:12pt;\">Fill space around images with black color.</span></p></body></html>", None))
|
||||
self.BorderBox.setText(_translate("KCC", "Black borders", None))
|
||||
self.BorderBox.setToolTip(_translate("KCC", "<html><head/><body><p><span style=\" font-weight:600; text-decoration: underline;\">Unchecked - Autodetection<br/></span>Color of margins fill will be detected automatically.</p><p><span style=\" font-weight:600; text-decoration: underline;\">Indeterminate - White<br/></span>Margins will be filled with white color.</p><p><span style=\" font-weight:600; text-decoration: underline;\">Checked - Black<br/></span>Margins will be filled with black color.</p></body></html>", None))
|
||||
self.BorderBox.setText(_translate("KCC", "W/B margins", None))
|
||||
self.NoRotateBox.setToolTip(_translate("KCC", "<html><head/><body><p><span style=\" font-size:12pt;\">Disable splitting and rotation.</span></p></body></html>", None))
|
||||
self.NoRotateBox.setText(_translate("KCC", "No split/rotate", None))
|
||||
self.DeviceBox.setToolTip(_translate("KCC", "Target device.", None))
|
||||
|
||||
@@ -275,9 +275,9 @@ def applyImgOptimization(img, options, overrideQuality=5):
|
||||
img.cutPageNumber()
|
||||
img.optimizeImage(options.gamma)
|
||||
if overrideQuality != 5:
|
||||
img.resizeImage(options.upscale, options.stretch, options.black_borders, overrideQuality)
|
||||
img.resizeImage(options.upscale, options.stretch, options.bordersColor, overrideQuality)
|
||||
else:
|
||||
img.resizeImage(options.upscale, options.stretch, options.black_borders, options.quality)
|
||||
img.resizeImage(options.upscale, options.stretch, options.bordersColor, options.quality)
|
||||
if options.forcepng and not options.forcecolor:
|
||||
img.quantizeImage()
|
||||
|
||||
@@ -804,7 +804,9 @@ def main(argv=None, qtGUI=None):
|
||||
experimentalOptions.add_option("-w", "--webtoon", action="store_true", dest="webtoon", default=False,
|
||||
help="Webtoon processing mode"),
|
||||
processingOptions.add_option("--blackborders", action="store_true", dest="black_borders", default=False,
|
||||
help="Use black borders instead of white ones")
|
||||
help="Disable autodetection and force black borders")
|
||||
processingOptions.add_option("--whiteborders", action="store_true", dest="white_borders", default=False,
|
||||
help="Disable autodetection and force white borders")
|
||||
processingOptions.add_option("--forcecolor", action="store_true", dest="forcecolor", default=False,
|
||||
help="Don't convert images to grayscale")
|
||||
processingOptions.add_option("--forcepng", action="store_true", dest="forcepng", default=False,
|
||||
@@ -919,6 +921,11 @@ def getOutputFilename(srcpath, wantedname, ext, tomeNumber):
|
||||
def checkOptions():
|
||||
global options
|
||||
options.panelview = True
|
||||
options.bordersColor = None
|
||||
if options.white_borders:
|
||||
options.bordersColor = "white"
|
||||
if options.black_borders:
|
||||
options.bordersColor = "black"
|
||||
# Disabling grayscale conversion for Kindle Fire family.
|
||||
if options.profile == 'KF' or options.profile == 'KFHD' or options.profile == 'KFHD8' or options.forcecolor:
|
||||
options.forcecolor = True
|
||||
@@ -931,7 +938,6 @@ def checkOptions():
|
||||
# Webtoon mode mandatory options
|
||||
if options.webtoon:
|
||||
options.nosplitrotate = True
|
||||
options.black_borders = False
|
||||
options.quality = 0
|
||||
options.panelview = False
|
||||
# Disable all Kindle features for other e-readers
|
||||
|
||||
@@ -53,45 +53,6 @@ def getImageFileName(imgfile):
|
||||
return filename
|
||||
|
||||
|
||||
def getImageHistogram(image):
|
||||
histogram = image.histogram()
|
||||
RBGW = []
|
||||
for i in range(256):
|
||||
RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
|
||||
white = 0
|
||||
black = 0
|
||||
for i in range(245, 256):
|
||||
white += RBGW[i]
|
||||
for i in range(11):
|
||||
black += RBGW[i]
|
||||
if white > black:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def getImageFill(image):
|
||||
imageSize = image.size
|
||||
imageT = image.crop((0, 0, imageSize[0], 1))
|
||||
imageB = image.crop((0, imageSize[1]-1, imageSize[0], imageSize[1]))
|
||||
fill = 0
|
||||
fill += getImageHistogram(imageT)
|
||||
fill += getImageHistogram(imageB)
|
||||
if fill == 2:
|
||||
return 'KCCFB'
|
||||
elif fill == 0:
|
||||
return 'KCCFW'
|
||||
else:
|
||||
imageL = image.crop((0, 0, 1, imageSize[1]))
|
||||
imageR = image.crop((imageSize[0]-1, 0, imageSize[0], imageSize[1]))
|
||||
fill += getImageHistogram(imageL)
|
||||
fill += getImageHistogram(imageR)
|
||||
if fill >= 2:
|
||||
return 'KCCFB'
|
||||
else:
|
||||
return 'KCCFW'
|
||||
|
||||
|
||||
def sanitizePanelSize(panel, options):
|
||||
newPanels = []
|
||||
if panel[2] > 8 * options.height:
|
||||
@@ -222,7 +183,7 @@ def splitImage(work):
|
||||
newPage.paste(panelImg, (0, targetHeight))
|
||||
targetHeight += panels[panel][2]
|
||||
newPage.save(os.path.join(path, fileExpanded[0] + '-' +
|
||||
str(pageNumber) + '-' + getImageFill(newPage) + '.png'), 'PNG')
|
||||
str(pageNumber) + '.png'), 'PNG')
|
||||
pageNumber += 1
|
||||
os.remove(filePath)
|
||||
|
||||
|
||||
51
kcc/image.py
51
kcc/image.py
@@ -186,17 +186,12 @@ class ComicPage:
|
||||
palImg.putpalette(self.palette)
|
||||
self.image = self.image.quantize(palette=palImg)
|
||||
|
||||
def resizeImage(self, upscale=False, stretch=False, black_borders=False, qualityMode=0):
|
||||
def resizeImage(self, upscale=False, stretch=False, bordersColor=None, qualityMode=0):
|
||||
method = Image.ANTIALIAS
|
||||
if '-KCCFW' in str(self.filename):
|
||||
fill = 'white'
|
||||
elif '-KCCFB' in str(self.filename):
|
||||
fill = 'black'
|
||||
if bordersColor:
|
||||
fill = bordersColor
|
||||
else:
|
||||
if black_borders:
|
||||
fill = 'black'
|
||||
else:
|
||||
fill = 'white'
|
||||
fill = self.getImageFill()
|
||||
if qualityMode == 0:
|
||||
size = (self.size[0], self.size[1])
|
||||
generateBorder = True
|
||||
@@ -379,4 +374,40 @@ class ComicPage:
|
||||
# print "Right crop: %s"%diff
|
||||
self.image = self.image.crop((0, 0, widthImg - diff, heightImg))
|
||||
# print "New size: %sx%s"%(self.image.size[0],self.image.size[1])
|
||||
return self.image
|
||||
return self.image
|
||||
|
||||
def getImageHistogram(self, image):
|
||||
histogram = image.histogram()
|
||||
RBGW = []
|
||||
for i in range(256):
|
||||
RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
|
||||
white = 0
|
||||
black = 0
|
||||
for i in range(245, 256):
|
||||
white += RBGW[i]
|
||||
for i in range(11):
|
||||
black += RBGW[i]
|
||||
if white > black:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def getImageFill(self):
|
||||
imageT = self.image.crop((0, 0, self.image.size[0], 1))
|
||||
imageB = self.image.crop((0, self.image.size[1]-1, self.image.size[0], self.image.size[1]))
|
||||
fill = 0
|
||||
fill += self.getImageHistogram(imageT)
|
||||
fill += self.getImageHistogram(imageB)
|
||||
if fill == 2:
|
||||
return 'black'
|
||||
elif fill == 0:
|
||||
return 'white'
|
||||
else:
|
||||
imageL = self.image.crop((0, 0, 1, self.image.size[1]))
|
||||
imageR = self.image.crop((self.image.size[0]-1, 0, self.image.size[0], self.image.size[1]))
|
||||
fill += self.getImageHistogram(imageL)
|
||||
fill += self.getImageHistogram(imageR)
|
||||
if fill >= 2:
|
||||
return 'black'
|
||||
else:
|
||||
return 'white'
|
||||
Reference in New Issue
Block a user