about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaweł Jastrzębski <pawelj@vulturis.eu>2013-09-15 16:24:14 +0200
committerPaweł Jastrzębski <pawelj@vulturis.eu>2013-09-15 16:24:14 +0200
commita8c3ef7d0034efea19635c78e4cf341a643fd50d (patch)
treef7102b3f2f6a6b716ced7f5e7e0fb8558206c3c7
parentMerged Kindle 3, 4 and 5 profiles (diff)
downloadkcc-a8c3ef7d0034efea19635c78e4cf341a643fd50d.tar.gz
kcc-a8c3ef7d0034efea19635c78e4cf341a643fd50d.tar.bz2
kcc-a8c3ef7d0034efea19635c78e4cf341a643fd50d.zip
Margins color detection now handles every file
-rw-r--r--KCC-OSX.ui7
-rw-r--r--KCC.ui7
-rw-r--r--kcc/KCC_gui.py6
-rw-r--r--kcc/KCC_ui.py7
-rw-r--r--kcc/KCC_ui_osx.py7
-rwxr-xr-xkcc/comic2ebook.py14
-rw-r--r--kcc/comic2panel.py41
-rwxr-xr-xkcc/image.py51
8 files changed, 74 insertions, 66 deletions
diff --git a/KCC-OSX.ui b/KCC-OSX.ui
index f11dc4a..33db33c 100644
--- a/KCC-OSX.ui
+++ b/KCC-OSX.ui
@@ -145,10 +145,13 @@
         <enum>Qt::NoFocus</enum>
        </property>
        <property name="toolTip">
-        <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:12pt;&quot;&gt;Fill space around images with black color.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+        <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Unchecked - Autodetection&lt;br/&gt;&lt;/span&gt;Color of margins fill will be detected automatically.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Indeterminate - White&lt;br/&gt;&lt;/span&gt;Margins will be filled with white color.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Checked - Black&lt;br/&gt;&lt;/span&gt;Margins will be filled with black color.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
        </property>
        <property name="text">
-        <string>Black borders</string>
+        <string>W/B margins</string>
+       </property>
+       <property name="tristate">
+        <bool>true</bool>
        </property>
       </widget>
      </item>
diff --git a/KCC.ui b/KCC.ui
index 5960e23..a627a2d 100644
--- a/KCC.ui
+++ b/KCC.ui
@@ -123,10 +123,13 @@
         <enum>Qt::NoFocus</enum>
        </property>
        <property name="toolTip">
-        <string>Fill space around images with black color.</string>
+        <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Unchecked - Autodetection&lt;br/&gt;&lt;/span&gt;Color of margins fill will be detected automatically.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Indeterminate - White&lt;br/&gt;&lt;/span&gt;Margins will be filled with white color.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Checked - Black&lt;br/&gt;&lt;/span&gt;Margins will be filled with black color.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
        </property>
        <property name="text">
-        <string>Black borders</string>
+        <string>W/B margins</string>
+       </property>
+       <property name="tristate">
+        <bool>true</bool>
        </property>
       </widget>
      </item>
diff --git a/kcc/KCC_gui.py b/kcc/KCC_gui.py
index 3e5e5f4..1f6e289 100644
--- a/kcc/KCC_gui.py
+++ b/kcc/KCC_gui.py
@@ -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():
diff --git a/kcc/KCC_ui.py b/kcc/KCC_ui.py
index 2589b64..2fd98d8 100644
--- a/kcc/KCC_ui.py
+++ b/kcc/KCC_ui.py
@@ -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))
diff --git a/kcc/KCC_ui_osx.py b/kcc/KCC_ui_osx.py
index 06a940b..84474e5 100644
--- a/kcc/KCC_ui_osx.py
+++ b/kcc/KCC_ui_osx.py
@@ -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))
diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py
index bc17424..d5aff24 100755
--- a/kcc/comic2ebook.py
+++ b/kcc/comic2ebook.py
@@ -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
diff --git a/kcc/comic2panel.py b/kcc/comic2panel.py
index d7b3503..f9a52b1 100644
--- a/kcc/comic2panel.py
+++ b/kcc/comic2panel.py
@@ -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)
 
diff --git a/kcc/image.py b/kcc/image.py
index 86abf44..b384ba6 100755
--- a/kcc/image.py
+++ b/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
\ No newline at end of file
+        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'
\ No newline at end of file