about summary refs log tree commit diff
path: root/kcc/image.py
diff options
context:
space:
mode:
authorPaweł Jastrzębski <pawelj@iosphe.re>2015-01-04 09:13:20 +0100
committerPaweł Jastrzębski <pawelj@iosphe.re>2015-01-04 09:13:20 +0100
commit3187ebb0544716215bf421449ab3d42d91d09f0b (patch)
tree6866a89c9fea7909c8d48ebcd413c0b206a5eb78 /kcc/image.py
parentWebToon processing improvements (diff)
downloadkcc-3187ebb0544716215bf421449ab3d42d91d09f0b.tar.gz
kcc-3187ebb0544716215bf421449ab3d42d91d09f0b.tar.bz2
kcc-3187ebb0544716215bf421449ab3d42d91d09f0b.zip
Generic processing improvements
Diffstat (limited to 'kcc/image.py')
-rwxr-xr-xkcc/image.py45
1 files changed, 29 insertions, 16 deletions
diff --git a/kcc/image.py b/kcc/image.py
index 9eff741..f8cc4af 100755
--- a/kcc/image.py
+++ b/kcc/image.py
@@ -102,16 +102,15 @@ class ProfileData:
 
 
 class ComicPage:
-    def __init__(self, source, device, fill=None):
+    def __init__(self, source, options, fill=None):
         try:
-            self.profile_label, self.size, self.palette, self.gamma, self.panelviewsize = device
+            self.profile_label, self.size, self.palette, self.gamma, self.panelviewsize = options.profileData
         except KeyError:
-            raise RuntimeError('Unexpected output device %s' % device)
+            raise RuntimeError('Unexpected output device %s' % options.profileData)
         self.origFileName = source
         self.filename = os.path.basename(self.origFileName)
         self.image = Image.open(source)
         self.image = self.image.convert('RGB')
-        self.color = self.isImageColor()
         self.rotated = None
         self.border = None
         self.noHPV = None
@@ -119,17 +118,22 @@ class ComicPage:
         self.noPV = None
         self.purge = False
         self.hq = False
+        self.opt = options
         if fill:
             self.fill = fill
         else:
             self.fill = None
+        if options.webtoon:
+            self.color = True
+        else:
+            self.color = self.isImageColor()
 
-    def saveToDir(self, targetdir, forcepng, color):
+    def saveToDir(self, targetdir):
         try:
             if not self.purge:
                 flags = []
                 filename = os.path.join(targetdir, os.path.splitext(self.filename)[0]) + '-KCC'
-                if not color and not forcepng:
+                if not self.opt.forcecolor and not self.opt.forcepng:
                     self.image = self.image.convert('L')
                 if self.rotated:
                     flags.append('Rotated')
@@ -146,7 +150,7 @@ class ComicPage:
                     if self.border:
                         flags.append('Margins-' + str(self.border[0]) + '-' + str(self.border[1]) + '-'
                                      + str(self.border[2]) + '-' + str(self.border[3]))
-                if forcepng:
+                if self.opt.forcepng:
                     filename += '.png'
                     self.image.save(filename, 'PNG', optimize=1)
                 else:
@@ -158,7 +162,8 @@ class ComicPage:
         except IOError as e:
             raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
 
-    def optimizeImage(self, gamma):
+    def optimizeImage(self):
+        gamma = self.opt.gamma
         if gamma < 0.1:
             gamma = self.gamma
             if self.gamma != 1.0 and self.color:
@@ -215,7 +220,12 @@ class ComicPage:
             self.noHPV = True
             self.noVPV = True
 
-    def resizeImage(self, upscale=False, stretch=False, bordersColor=None, qualityMode=0):
+    def resizeImage(self, qualityMode=None):
+        upscale = self.opt.upscale
+        stretch = self.opt.stretch
+        bordersColor = self.opt.bordersColor
+        if qualityMode is None:
+            qualityMode = self.opt.quality
         if bordersColor:
             fill = bordersColor
         else:
@@ -273,12 +283,12 @@ class ComicPage:
         self.image = ImageOps.fit(self.image, size, method=method, centering=(0.5, 0.5))
         return self.image
 
-    def splitPage(self, targetdir, righttoleft=False, rotate=False):
+    def splitPage(self, targetdir):
         width, height = self.image.size
         dstwidth, dstheight = self.size
         # Only split if origin is not oriented the same as target
         if (width > height) != (dstwidth > dstheight):
-            if rotate:
+            if self.opt.rotate:
                 self.image = self.image.rotate(90, Image.BICUBIC, True)
                 self.rotated = True
                 return None
@@ -296,7 +306,7 @@ class ComicPage:
                 fileone = targetdir + '/' + filename + '-AAA.png'
                 filetwo = targetdir + '/' + filename + '-BBB.png'
                 try:
-                    if righttoleft:
+                    if self.opt.righttoleft:
                         pageone = self.image.crop(rightbox)
                         pagetwo = self.image.crop(leftbox)
                     else:
@@ -417,13 +427,16 @@ class ComicPage:
         imageBoxB = ImageChops.invert(bw).getbbox()
         if imageBoxA is None or imageBoxB is None:
             surfaceB, surfaceW = 0, 0
+            diff = 0
         else:
             surfaceB = (imageBoxA[2] - imageBoxA[0]) * (imageBoxA[3] - imageBoxA[1])
             surfaceW = (imageBoxB[2] - imageBoxB[0]) * (imageBoxB[3] - imageBoxB[1])
-        if surfaceW < surfaceB:
-            self.fill = 'white'
-        elif surfaceW > surfaceB:
-            self.fill = 'black'
+            diff = ((max(surfaceB, surfaceW) - min(surfaceB, surfaceW)) / min(surfaceB, surfaceW)) * 100
+        if diff > 0.5:
+            if surfaceW < surfaceB:
+                self.fill = 'white'
+            elif surfaceW > surfaceB:
+                self.fill = 'black'
         else:
             fill = 0
             startY = 0