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
+16 -16
View File
@@ -444,12 +444,12 @@ def imgOptimization(img, opt, hqImage=None):
img.cropWhiteSpace() img.cropWhiteSpace()
if opt.cutpagenumbers and not opt.webtoon: if opt.cutpagenumbers and not opt.webtoon:
img.cutPageNumber() img.cutPageNumber()
img.optimizeImage(opt.gamma) img.optimizeImage()
if hqImage: if hqImage:
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, 0) img.resizeImage(0)
img.calculateBorder(hqImage, True) img.calculateBorder(hqImage, True)
else: else:
img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, opt.quality) img.resizeImage()
if opt.panelview: if opt.panelview:
if opt.quality == 0: if opt.quality == 0:
img.calculateBorder(img) img.calculateBorder(img)
@@ -515,7 +515,7 @@ def imgFileProcessing(work):
dirpath = work[1] dirpath = work[1]
opt = work[2] opt = work[2]
output = [] output = []
img = image.ComicPage(os.path.join(dirpath, afile), opt.profileData) img = image.ComicPage(os.path.join(dirpath, afile), opt)
if opt.quality == 2: if opt.quality == 2:
wipe = False wipe = False
else: else:
@@ -523,39 +523,39 @@ def imgFileProcessing(work):
if opt.nosplitrotate: if opt.nosplitrotate:
splitter = None splitter = None
else: else:
splitter = img.splitPage(dirpath, opt.righttoleft, opt.rotate) splitter = img.splitPage(dirpath)
if splitter is not None: if splitter is not None:
img0 = image.ComicPage(splitter[0], opt.profileData) img0 = image.ComicPage(splitter[0], opt)
imgOptimization(img0, opt) imgOptimization(img0, opt)
output.append(img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img0.saveToDir(dirpath))
img1 = image.ComicPage(splitter[1], opt.profileData) img1 = image.ComicPage(splitter[1], opt)
imgOptimization(img1, opt) imgOptimization(img1, opt)
output.append(img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img1.saveToDir(dirpath))
if wipe: if wipe:
output.append(img0.origFileName) output.append(img0.origFileName)
output.append(img1.origFileName) output.append(img1.origFileName)
if opt.quality == 2: 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) imgOptimization(img0b, opt, img0)
output.append(img0b.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img0b.saveToDir(dirpath))
img1b = image.ComicPage(splitter[1], opt.profileData, img1.fill) img1b = image.ComicPage(splitter[1], opt, img1.fill)
imgOptimization(img1b, opt, img1) imgOptimization(img1b, opt, img1)
output.append(img1b.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img1b.saveToDir(dirpath))
output.append(img0.origFileName) output.append(img0.origFileName)
output.append(img1.origFileName) output.append(img1.origFileName)
output.append(img.origFileName) output.append(img.origFileName)
else: else:
imgOptimization(img, opt) imgOptimization(img, opt)
output.append(img.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img.saveToDir(dirpath))
if wipe: if wipe:
output.append(img.origFileName) output.append(img.origFileName)
if opt.quality == 2: 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: if img.rotated:
img2.image = img2.image.rotate(90, Image.BICUBIC, True) img2.image = img2.image.rotate(90, Image.BICUBIC, True)
img2.rotated = True img2.rotated = True
imgOptimization(img2, opt, img) imgOptimization(img2, opt, img)
output.append(img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) output.append(img2.saveToDir(dirpath))
output.append(img.origFileName) output.append(img.origFileName)
return output return output
except Exception: except Exception:
+29 -16
View File
@@ -102,16 +102,15 @@ class ProfileData:
class ComicPage: class ComicPage:
def __init__(self, source, device, fill=None): def __init__(self, source, options, fill=None):
try: 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: except KeyError:
raise RuntimeError('Unexpected output device %s' % device) raise RuntimeError('Unexpected output device %s' % options.profileData)
self.origFileName = source self.origFileName = source
self.filename = os.path.basename(self.origFileName) self.filename = os.path.basename(self.origFileName)
self.image = Image.open(source) self.image = Image.open(source)
self.image = self.image.convert('RGB') self.image = self.image.convert('RGB')
self.color = self.isImageColor()
self.rotated = None self.rotated = None
self.border = None self.border = None
self.noHPV = None self.noHPV = None
@@ -119,17 +118,22 @@ class ComicPage:
self.noPV = None self.noPV = None
self.purge = False self.purge = False
self.hq = False self.hq = False
self.opt = options
if fill: if fill:
self.fill = fill self.fill = fill
else: else:
self.fill = None 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: try:
if not self.purge: if not self.purge:
flags = [] flags = []
filename = os.path.join(targetdir, os.path.splitext(self.filename)[0]) + '-KCC' 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') self.image = self.image.convert('L')
if self.rotated: if self.rotated:
flags.append('Rotated') flags.append('Rotated')
@@ -146,7 +150,7 @@ class ComicPage:
if self.border: if self.border:
flags.append('Margins-' + str(self.border[0]) + '-' + str(self.border[1]) + '-' flags.append('Margins-' + str(self.border[0]) + '-' + str(self.border[1]) + '-'
+ str(self.border[2]) + '-' + str(self.border[3])) + str(self.border[2]) + '-' + str(self.border[3]))
if forcepng: if self.opt.forcepng:
filename += '.png' filename += '.png'
self.image.save(filename, 'PNG', optimize=1) self.image.save(filename, 'PNG', optimize=1)
else: else:
@@ -158,7 +162,8 @@ class ComicPage:
except IOError as e: except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, 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: if gamma < 0.1:
gamma = self.gamma gamma = self.gamma
if self.gamma != 1.0 and self.color: if self.gamma != 1.0 and self.color:
@@ -215,7 +220,12 @@ class ComicPage:
self.noHPV = True self.noHPV = True
self.noVPV = 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: if bordersColor:
fill = bordersColor fill = bordersColor
else: else:
@@ -273,12 +283,12 @@ class ComicPage:
self.image = ImageOps.fit(self.image, size, method=method, centering=(0.5, 0.5)) self.image = ImageOps.fit(self.image, size, method=method, centering=(0.5, 0.5))
return self.image return self.image
def splitPage(self, targetdir, righttoleft=False, rotate=False): def splitPage(self, targetdir):
width, height = self.image.size width, height = self.image.size
dstwidth, dstheight = self.size dstwidth, dstheight = self.size
# Only split if origin is not oriented the same as target # Only split if origin is not oriented the same as target
if (width > height) != (dstwidth > dstheight): if (width > height) != (dstwidth > dstheight):
if rotate: if self.opt.rotate:
self.image = self.image.rotate(90, Image.BICUBIC, True) self.image = self.image.rotate(90, Image.BICUBIC, True)
self.rotated = True self.rotated = True
return None return None
@@ -296,7 +306,7 @@ class ComicPage:
fileone = targetdir + '/' + filename + '-AAA.png' fileone = targetdir + '/' + filename + '-AAA.png'
filetwo = targetdir + '/' + filename + '-BBB.png' filetwo = targetdir + '/' + filename + '-BBB.png'
try: try:
if righttoleft: if self.opt.righttoleft:
pageone = self.image.crop(rightbox) pageone = self.image.crop(rightbox)
pagetwo = self.image.crop(leftbox) pagetwo = self.image.crop(leftbox)
else: else:
@@ -417,13 +427,16 @@ class ComicPage:
imageBoxB = ImageChops.invert(bw).getbbox() imageBoxB = ImageChops.invert(bw).getbbox()
if imageBoxA is None or imageBoxB is None: if imageBoxA is None or imageBoxB is None:
surfaceB, surfaceW = 0, 0 surfaceB, surfaceW = 0, 0
diff = 0
else: else:
surfaceB = (imageBoxA[2] - imageBoxA[0]) * (imageBoxA[3] - imageBoxA[1]) surfaceB = (imageBoxA[2] - imageBoxA[0]) * (imageBoxA[3] - imageBoxA[1])
surfaceW = (imageBoxB[2] - imageBoxB[0]) * (imageBoxB[3] - imageBoxB[1]) surfaceW = (imageBoxB[2] - imageBoxB[0]) * (imageBoxB[3] - imageBoxB[1])
if surfaceW < surfaceB: diff = ((max(surfaceB, surfaceW) - min(surfaceB, surfaceW)) / min(surfaceB, surfaceW)) * 100
self.fill = 'white' if diff > 0.5:
elif surfaceW > surfaceB: if surfaceW < surfaceB:
self.fill = 'black' self.fill = 'white'
elif surfaceW > surfaceB:
self.fill = 'black'
else: else:
fill = 0 fill = 0
startY = 0 startY = 0