1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-22 14:11:45 +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,72 +413,45 @@ 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 > 0 and black == 0: elif white > pixelCount*0.5 and black == 0:
return -1 return -1
else: else:
return False return False
else:
if black > white and black > pixelCount*0.5:
return True
else:
return False
def getImageFill(self, isWebToon): def getImageFill(self):
if isWebToon:
fill = 0
fill += self.getImageHistogram(self.image.crop((0, 0, self.image.size[0], 5)), False)
fill += self.getImageHistogram(self.image.crop((0, self.image.size[1]-5, self.image.size[0],
self.image.size[1])), False)
if fill == 2:
self.fill = 'black'
elif fill == 0:
self.fill = 'white'
else:
fill = 0
fill += self.getImageHistogram(self.image.crop((0, 0, 5, 5)), False)
fill += self.getImageHistogram(self.image.crop((self.image.size[0]-5, 0, self.image.size[0], 5)), False)
fill += self.getImageHistogram(self.image.crop((0, self.image.size[1]-5, 5, self.image.size[1])), False)
fill += self.getImageHistogram(self.image.crop((self.image.size[0]-5, self.image.size[1]-5,
self.image.size[0], self.image.size[1])), False)
if fill > 1:
self.fill = 'black'
else:
self.fill = 'white'
else:
fill = 0 fill = 0
# Search fom horizontal solid lines # Search fom horizontal solid lines
startY = 0 startY = 0
stopY = 3 stopY = 4
searching = True searching = True
while stopY <= self.image.size[1]: while stopY <= self.image.size[1]:
checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], stopY))) checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], stopY)))
if checkSolid: if checkSolid:
fill += checkSolid fill += checkSolid
startY = stopY + 1 startY = stopY + 1
stopY = startY + 3 stopY = startY + 4
if stopY > self.image.size[1] and searching: if stopY > self.image.size[1] and searching:
startY = self.image.size[1] - 3 startY = self.image.size[1] - 4
stopY = self.image.size[1] stopY = self.image.size[1]
searching = False searching = False
# Search fom vertical solid lines # Search fom vertical solid lines
startX = 0 startX = 0
stopX = 3 stopX = 4
searching = True searching = True
while stopX <= self.image.size[0]: while stopX <= self.image.size[0]:
checkSolid = self.getImageHistogram(self.image.crop((startX, 0, stopX, self.image.size[1]))) checkSolid = self.getImageHistogram(self.image.crop((startX, 0, stopX, self.image.size[1])))
if checkSolid: if checkSolid:
fill += checkSolid fill += checkSolid
startX = stopX + 1 startX = stopX + 1
stopX = startX + 3 stopX = startX + 4
if stopX > self.image.size[0] and searching: if stopX > self.image.size[0] and searching:
startX = self.image.size[0] - 3 startX = self.image.size[0] - 4
stopX = self.image.size[0] stopX = self.image.size[0]
searching = False searching = False
if fill > 0: if fill > 0: