1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-16 03:06:33 +00:00

Generic processing improvements

This commit is contained in:
Paweł Jastrzębski
2015-01-04 09:13:20 +01:00
parent b9276e9ede
commit 3187ebb054
2 changed files with 45 additions and 32 deletions

View File

@@ -444,12 +444,12 @@ def imgOptimization(img, opt, hqImage=None):
img.cropWhiteSpace()
if opt.cutpagenumbers and not opt.webtoon:
img.cutPageNumber()
img.optimizeImage(opt.gamma)
img.optimizeImage()
if hqImage:
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, 0)
img.resizeImage(0)
img.calculateBorder(hqImage, True)
else:
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, opt.quality)
img.resizeImage()
if opt.panelview:
if opt.quality == 0:
img.calculateBorder(img)
@@ -515,7 +515,7 @@ def imgFileProcessing(work):
dirpath = work[1]
opt = work[2]
output = []
img = image.ComicPage(os.path.join(dirpath, afile), opt.profileData)
img = image.ComicPage(os.path.join(dirpath, afile), opt)
if opt.quality == 2:
wipe = False
else:
@@ -523,39 +523,39 @@ def imgFileProcessing(work):
if opt.nosplitrotate:
splitter = None
else:
splitter = img.splitPage(dirpath, opt.righttoleft, opt.rotate)
splitter = img.splitPage(dirpath)
if splitter is not None:
img0 = image.ComicPage(splitter[0], opt.profileData)
img0 = image.ComicPage(splitter[0], opt)
imgOptimization(img0, opt)
output.append(img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
img1 = image.ComicPage(splitter[1], opt.profileData)
output.append(img0.saveToDir(dirpath))
img1 = image.ComicPage(splitter[1], opt)
imgOptimization(img1, opt)
output.append(img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
output.append(img1.saveToDir(dirpath))
if wipe:
output.append(img0.origFileName)
output.append(img1.origFileName)
if opt.quality == 2:
img0b = image.ComicPage(splitter[0], opt.profileData, img0.fill)
img0b = image.ComicPage(splitter[0], opt, img0.fill)
imgOptimization(img0b, opt, img0)
output.append(img0b.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
img1b = image.ComicPage(splitter[1], opt.profileData, img1.fill)
output.append(img0b.saveToDir(dirpath))
img1b = image.ComicPage(splitter[1], opt, img1.fill)
imgOptimization(img1b, opt, img1)
output.append(img1b.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
output.append(img1b.saveToDir(dirpath))
output.append(img0.origFileName)
output.append(img1.origFileName)
output.append(img.origFileName)
else:
imgOptimization(img, opt)
output.append(img.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
output.append(img.saveToDir(dirpath))
if wipe:
output.append(img.origFileName)
if opt.quality == 2:
img2 = image.ComicPage(os.path.join(dirpath, afile), opt.profileData, img.fill)
img2 = image.ComicPage(os.path.join(dirpath, afile), opt, img.fill)
if img.rotated:
img2.image = img2.image.rotate(90, Image.BICUBIC, True)
img2.rotated = True
imgOptimization(img2, opt, img)
output.append(img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor))
output.append(img2.saveToDir(dirpath))
output.append(img.origFileName)
return output
except Exception:

View File

@@ -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