1
0
mirror of https://github.com/ciromattia/kcc synced 2026-01-27 23:47:41 +00:00

comic2panel: Autodetect border color

This commit is contained in:
Paweł Jastrzębski
2013-08-11 14:47:06 +02:00
parent d3e0c2bb6e
commit 2865915cdf
2 changed files with 26 additions and 8 deletions

View File

@@ -89,6 +89,19 @@ def splitImage(work):
debugImage = Image.open(os.path.join(path, name))
draw = ImageDraw.Draw(debugImage)
# Find fill color
white = 0
black = 0
for i in image.getdata():
if i == (255, 255, 255):
white += 1
elif i == (0, 0, 0):
black += 1
if white > black:
fill = 'KCCFW'
else:
fill = 'KCCFB'
# Find panels
y1 = 0
y2 = 10
@@ -148,7 +161,7 @@ def splitImage(work):
panelImg = image.crop([0, panels[panel][0], widthImg, panels[panel][1]])
newPage.paste(panelImg, (0, targetHeight))
targetHeight += panels[panel][2]
newPage.save(os.path.join(path, fileExpanded[0] + '-' + str(pageNumber) + '.png'), 'PNG')
newPage.save(os.path.join(path, fileExpanded[0] + '-' + str(pageNumber) + '-' + fill + '.png'), 'PNG')
pageNumber += 1
print ".",
os.remove(os.path.join(path, name))

View File

@@ -115,6 +115,7 @@ class ComicPage:
# Detect corrupted files - Phase 2
try:
self.origFileName = source
self.filename = os.path.basename(self.origFileName)
self.image = Image.open(source)
except IOError:
raise RuntimeError('Cannot read image file %s' % source)
@@ -134,7 +135,6 @@ class ComicPage:
self.image = self.image.convert('RGB')
def saveToDir(self, targetdir, forcepng, color, wipe, suffix=None):
filename = os.path.basename(self.origFileName)
try:
if not color:
self.image = self.image.convert('L') # convert to grayscale
@@ -143,13 +143,13 @@ class ComicPage:
else:
suffix = ""
if wipe:
os.remove(os.path.join(targetdir, filename))
os.remove(os.path.join(targetdir, self.filename))
else:
suffix += "_kcchq"
if forcepng:
self.image.save(os.path.join(targetdir, os.path.splitext(filename)[0] + suffix + ".png"), "PNG")
self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".png"), "PNG")
else:
self.image.save(os.path.join(targetdir, os.path.splitext(filename)[0] + suffix + ".jpg"), "JPEG")
self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".jpg"), "JPEG")
except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
@@ -174,10 +174,15 @@ class ComicPage:
def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False,
landscapeMode=False, qualityMode=0):
method = Image.ANTIALIAS
if black_borders:
if '-KCCFW' in str(self.filename):
fill = 'white'
elif '-KCCFB' in str(self.filename):
fill = 'black'
else:
fill = 'white'
if black_borders:
fill = 'black'
else:
fill = 'white'
if qualityMode == 0:
size = (self.size[0], self.size[1])
else:
@@ -234,7 +239,7 @@ class ComicPage:
# source is portrait and target is landscape, so split by the height
leftbox = (0, 0, width, height / 2)
rightbox = (0, height / 2, width, height)
filename = os.path.splitext(os.path.basename(self.origFileName))
filename = os.path.splitext(self.filename)
fileone = targetdir + '/' + filename[0] + '_kcca' + filename[1]
filetwo = targetdir + '/' + filename[0] + '_kccb' + filename[1]
try: