1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-22 06:01:49 +00:00

Margin autodetection improvements

This commit is contained in:
Paweł Jastrzębski
2013-12-28 12:08:33 +01:00
parent 93f5d105cf
commit 3cc99c6221
2 changed files with 42 additions and 69 deletions

View File

@@ -310,7 +310,7 @@ def getImageFileName(imgfile):
def applyImgOptimization(img, opt, hqImage=None): def applyImgOptimization(img, opt, hqImage=None):
if not img.fill: if not img.fill:
img.getImageFill(opt.webtoon) img.getImageFill()
if not opt.webtoon: if not opt.webtoon:
img.cropWhiteSpace(10.0) img.cropWhiteSpace(10.0)
if opt.cutpagenumbers and not opt.webtoon: if opt.cutpagenumbers and not opt.webtoon:

View File

@@ -404,7 +404,7 @@ class ComicPage:
self.image = self.image.crop((0, 0, widthImg - diff, heightImg)) self.image = self.image.crop((0, 0, widthImg - diff, heightImg))
return self.image return self.image
def getImageHistogram(self, image, new=True): def getImageHistogram(self, image):
histogram = image.histogram() histogram = image.histogram()
RBGW = [] RBGW = []
pixelCount = 0 pixelCount = 0
@@ -413,78 +413,51 @@ class ComicPage:
RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i]) RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
white = 0 white = 0
black = 0 black = 0
for i in range(251, 256): for i in range(253, 256):
white += RBGW[i] white += RBGW[i]
for i in range(5): for i in range(3):
black += RBGW[i] black += RBGW[i]
if new: if black > pixelCount*0.5 and white == 0:
if black > 0 and white == 0: return 1
return 1 elif white > pixelCount*0.5 and black == 0:
elif white > 0 and black == 0: return -1
return -1
else:
return False
else: else:
if black > white and black > pixelCount*0.5: return False
return True
else:
return False
def getImageFill(self, isWebToon): def getImageFill(self):
if isWebToon: fill = 0
fill = 0 # Search fom horizontal solid lines
fill += self.getImageHistogram(self.image.crop((0, 0, self.image.size[0], 5)), False) startY = 0
fill += self.getImageHistogram(self.image.crop((0, self.image.size[1]-5, self.image.size[0], stopY = 4
self.image.size[1])), False) searching = True
if fill == 2: while stopY <= self.image.size[1]:
self.fill = 'black' checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], stopY)))
elif fill == 0: if checkSolid:
self.fill = 'white' fill += checkSolid
else: startY = stopY + 1
fill = 0 stopY = startY + 4
fill += self.getImageHistogram(self.image.crop((0, 0, 5, 5)), False) if stopY > self.image.size[1] and searching:
fill += self.getImageHistogram(self.image.crop((self.image.size[0]-5, 0, self.image.size[0], 5)), False) startY = self.image.size[1] - 4
fill += self.getImageHistogram(self.image.crop((0, self.image.size[1]-5, 5, self.image.size[1])), False) stopY = self.image.size[1]
fill += self.getImageHistogram(self.image.crop((self.image.size[0]-5, self.image.size[1]-5, searching = False
self.image.size[0], self.image.size[1])), False) # Search fom vertical solid lines
if fill > 1: startX = 0
self.fill = 'black' stopX = 4
else: searching = True
self.fill = 'white' while stopX <= self.image.size[0]:
checkSolid = self.getImageHistogram(self.image.crop((startX, 0, stopX, self.image.size[1])))
if checkSolid:
fill += checkSolid
startX = stopX + 1
stopX = startX + 4
if stopX > self.image.size[0] and searching:
startX = self.image.size[0] - 4
stopX = self.image.size[0]
searching = False
if fill > 0:
self.fill = 'black'
else: else:
fill = 0 self.fill = 'white'
# Search fom horizontal solid lines
startY = 0
stopY = 3
searching = True
while stopY <= self.image.size[1]:
checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], stopY)))
if checkSolid:
fill += checkSolid
startY = stopY + 1
stopY = startY + 3
if stopY > self.image.size[1] and searching:
startY = self.image.size[1] - 3
stopY = self.image.size[1]
searching = False
# Search fom vertical solid lines
startX = 0
stopX = 3
searching = True
while stopX <= self.image.size[0]:
checkSolid = self.getImageHistogram(self.image.crop((startX, 0, stopX, self.image.size[1])))
if checkSolid:
fill += checkSolid
startX = stopX + 1
stopX = startX + 3
if stopX > self.image.size[0] and searching:
startX = self.image.size[0] - 3
stopX = self.image.size[0]
searching = False
if fill > 0:
self.fill = 'black'
else:
self.fill = 'white'
def isImageColor(self, image): def isImageColor(self, image):
v = ImageStat.Stat(image).var v = ImageStat.Stat(image).var