mirror of
https://github.com/ciromattia/kcc
synced 2025-12-13 09:46:25 +00:00
Implemented new method to detect color images
This commit is contained in:
38
kcc/image.py
38
kcc/image.py
@@ -442,29 +442,25 @@ 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))
|
||||||
return False
|
SSE, bias = 0, [0, 0, 0]
|
||||||
else:
|
bias = ImageStat.Stat(thumb).mean[:3]
|
||||||
if len(v) == 3:
|
bias = [b - sum(bias) / 3 for b in bias]
|
||||||
maxmin = abs(max(v) - min(v))
|
for pixel in thumb.getdata():
|
||||||
if maxmin > 1000:
|
mu = sum(pixel) / 3
|
||||||
# Color
|
SSE += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
|
||||||
return True
|
MSE = float(SSE) / (40 * 40)
|
||||||
elif maxmin > 100:
|
if MSE <= 22:
|
||||||
# Probably color
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
# Grayscale
|
|
||||||
return False
|
|
||||||
elif len(v) == 1:
|
|
||||||
# Black and white
|
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
# Detection failed
|
return True
|
||||||
return False
|
elif len(bands) == 1:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Cover:
|
class Cover:
|
||||||
|
|||||||
Reference in New Issue
Block a user