mirror of
https://github.com/ciromattia/kcc
synced 2025-12-13 09:46:25 +00:00
Replaced margin color detection algorithm
This commit is contained in:
@@ -425,7 +425,7 @@ def buildEPUB(path, chapterNames, tomeNumber):
|
|||||||
|
|
||||||
def imgOptimization(img, opt, hqImage=None):
|
def imgOptimization(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()
|
img.cropWhiteSpace()
|
||||||
if opt.cutpagenumbers and not opt.webtoon:
|
if opt.cutpagenumbers and not opt.webtoon:
|
||||||
|
|||||||
62
kcc/image.py
62
kcc/image.py
@@ -404,45 +404,39 @@ class ComicPage:
|
|||||||
|
|
||||||
def getImageHistogram(self, image):
|
def getImageHistogram(self, image):
|
||||||
histogram = image.histogram()
|
histogram = image.histogram()
|
||||||
RBGW = []
|
if histogram[0] == 0:
|
||||||
pixelCount = 0
|
|
||||||
for i in range(256):
|
|
||||||
pixelCount += histogram[i] + histogram[256 + i] + histogram[512 + i]
|
|
||||||
RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
|
|
||||||
white = 0
|
|
||||||
black = 0
|
|
||||||
for i in range(251, 256):
|
|
||||||
white += RBGW[i]
|
|
||||||
for i in range(5):
|
|
||||||
black += RBGW[i]
|
|
||||||
if black > pixelCount*0.8 and white == 0:
|
|
||||||
return 1
|
|
||||||
elif white > pixelCount*0.8 and black == 0:
|
|
||||||
return -1
|
return -1
|
||||||
|
elif histogram[255] == 0:
|
||||||
|
return 1
|
||||||
else:
|
else:
|
||||||
return False
|
return 0
|
||||||
|
|
||||||
def getImageFill(self, webtoon):
|
def getImageFill(self):
|
||||||
fill = 0
|
bw = self.image.convert('L').point(lambda x: 0 if x < 128 else 255, '1')
|
||||||
if not webtoon and not self.rotated:
|
imageBoxA = bw.getbbox()
|
||||||
# Search for horizontal solid lines
|
imageBoxB = ImageChops.invert(bw).getbbox()
|
||||||
startY = 0
|
if imageBoxA is None or imageBoxB is None:
|
||||||
while startY < self.image.size[1]:
|
surfaceB, surfaceW = 0, 0
|
||||||
if startY + 5 > self.image.size[1]:
|
|
||||||
startY = self.image.size[1] - 5
|
|
||||||
checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], startY+5)))
|
|
||||||
if checkSolid:
|
|
||||||
fill += checkSolid
|
|
||||||
startY += 5
|
|
||||||
else:
|
else:
|
||||||
# Search for vertical solid lines
|
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'
|
||||||
|
else:
|
||||||
|
fill = 0
|
||||||
|
startY = 0
|
||||||
|
while startY < bw.size[1]:
|
||||||
|
if startY + 5 > bw.size[1]:
|
||||||
|
startY = bw.size[1] - 5
|
||||||
|
fill += self.getImageHistogram(bw.crop((0, startY, bw.size[0], startY+5)))
|
||||||
|
startY += 5
|
||||||
startX = 0
|
startX = 0
|
||||||
while startX < self.image.size[0]:
|
while startX < bw.size[0]:
|
||||||
if startX + 5 > self.image.size[0]:
|
if startX + 5 > bw.size[0]:
|
||||||
startX = self.image.size[0] - 5
|
startX = bw.size[0] - 5
|
||||||
checkSolid = self.getImageHistogram(self.image.crop((startX, 0, startX+5, self.image.size[1])))
|
fill += self.getImageHistogram(bw.crop((startX, 0, startX+5, bw.size[1])))
|
||||||
if checkSolid:
|
|
||||||
fill += checkSolid
|
|
||||||
startX += 5
|
startX += 5
|
||||||
if fill > 0:
|
if fill > 0:
|
||||||
self.fill = 'black'
|
self.fill = 'black'
|
||||||
|
|||||||
Reference in New Issue
Block a user