1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 09:46:25 +00:00

comic2panel: Detection of corrupted files

This commit is contained in:
Paweł Jastrzębski
2013-08-16 19:25:26 +02:00
parent 61c0b691ab
commit 6eaf8cc374

View File

@@ -70,16 +70,23 @@ def getImageHistogram(image):
return True return True
def getImageFill(image): def getImageFill(image, y):
imageSize = image.size imageSize = image.size
imageT = image.crop((0, 0, imageSize[0], 1)) image = image.crop((0, 0, imageSize[0], y))
imageB = image.crop((0, imageSize[1]-1, imageSize[0], imageSize[1])) histogram = image.histogram()
imageT = getImageHistogram(imageT) RBGW = []
imageB = getImageHistogram(imageB) for i in range(256):
if imageT or imageB: RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
return 'KCCFB' white = 0
else: black = 0
for i in range(245, 256):
white += RBGW[i]
for i in range(11):
black += RBGW[i]
if white > black:
return 'KCCFW' return 'KCCFW'
else:
return 'KCCFB'
def sanitizePanelSize(panel, options): def sanitizePanelSize(panel, options):
@@ -124,18 +131,36 @@ def splitImage(work):
print ".", print ".",
splitImage.queue.put(".") splitImage.queue.put(".")
fileExpanded = os.path.splitext(name) fileExpanded = os.path.splitext(name)
image = Image.open(os.path.join(path, name)) filePath = os.path.join(path, name)
# Detect corrupted files
try:
image = Image.open(filePath)
except IOError:
raise RuntimeError('Cannot read image file %s' % filePath)
try:
image = Image.open(filePath)
image.verify()
except:
raise RuntimeError('Image file %s is corrupted' % filePath)
try:
image = Image.open(filePath)
image.load()
except:
raise RuntimeError('Image file %s is corrupted' % filePath)
image = Image.open(filePath)
image = image.convert('RGB') image = image.convert('RGB')
widthImg, heightImg = image.size widthImg, heightImg = image.size
if heightImg > options.height: if heightImg > options.height:
if options.debug: if options.debug:
from PIL import ImageDraw from PIL import ImageDraw
debugImage = Image.open(os.path.join(path, name)) debugImage = Image.open(filePath)
draw = ImageDraw.Draw(debugImage) draw = ImageDraw.Draw(debugImage)
# Find panels # Find panels
y1 = 0 y1 = 0
y2 = 15 y2 = 15
fillDataNeeded = True
yFill = 1
panels = [] panels = []
while y2 < heightImg: while y2 < heightImg:
while ImageStat.Stat(image.crop([0, y1, widthImg, y2])).var[0] < threshold and y2 < heightImg: while ImageStat.Stat(image.crop([0, y1, widthImg, y2])).var[0] < threshold and y2 < heightImg:
@@ -153,10 +178,14 @@ def splitImage(work):
draw.line([(0, y2Temp), (widthImg, y2Temp)], fill=(255, 0, 0)) draw.line([(0, y2Temp), (widthImg, y2Temp)], fill=(255, 0, 0))
panelHeight = y2Temp - y1Temp panelHeight = y2Temp - y1Temp
if y2Temp < heightImg: if y2Temp < heightImg:
if fillDataNeeded:
fillDataNeeded = False
yFill = y1Temp
# Panels that can't be cut nicely will be forcefully splitted # Panels that can't be cut nicely will be forcefully splitted
panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight], options) panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight], options)
for panel in panelsCleaned: for panel in panelsCleaned:
panels.append(panel) panels.append(panel)
fill = getImageFill(image, yFill)
if options.debug: if options.debug:
# noinspection PyUnboundLocalVariable # noinspection PyUnboundLocalVariable
debugImage.save(os.path.join(path, fileExpanded[0] + '-debug.png'), 'PNG') debugImage.save(os.path.join(path, fileExpanded[0] + '-debug.png'), 'PNG')
@@ -194,9 +223,9 @@ def splitImage(work):
newPage.paste(panelImg, (0, targetHeight)) newPage.paste(panelImg, (0, targetHeight))
targetHeight += panels[panel][2] targetHeight += panels[panel][2]
newPage.save(os.path.join(path, fileExpanded[0] + '-' + newPage.save(os.path.join(path, fileExpanded[0] + '-' +
str(pageNumber) + '-' + getImageFill(newPage) + '.png'), 'PNG') str(pageNumber) + '-' + fill + '.png'), 'PNG')
pageNumber += 1 pageNumber += 1
os.remove(os.path.join(path, name)) os.remove(filePath)
def Copyright(): def Copyright():