1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-15 18:56:28 +00:00

Implemented new method to detect color images

This commit is contained in:
Paweł Jastrzębski
2015-09-04 16:06:18 +02:00
parent 57e9637c81
commit 0988601842

View File

@@ -442,28 +442,24 @@ class ComicPage:
self.fill = 'white' self.fill = 'white'
def isImageColor(self): def isImageColor(self):
v = ImageStat.Stat(self.image).var img = self.image.copy()
isMonochromatic = reduce(lambda x, y: x and y < 0.005, v, True) bands = img.getbands()
if isMonochromatic: if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
# Monochromatic thumb = img.resize((40, 40))
SSE, bias = 0, [0, 0, 0]
bias = ImageStat.Stat(thumb).mean[:3]
bias = [b - sum(bias) / 3 for b in bias]
for pixel in thumb.getdata():
mu = sum(pixel) / 3
SSE += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
MSE = float(SSE) / (40 * 40)
if MSE <= 22:
return False return False
else: else:
if len(v) == 3:
maxmin = abs(max(v) - min(v))
if maxmin > 1000:
# Color
return True return True
elif maxmin > 100: elif len(bands) == 1:
# Probably color
return True
else:
# Grayscale
return False
elif len(v) == 1:
# Black and white
return False return False
else: else:
# Detection failed
return False return False