-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbw_detector.py
29 lines (28 loc) · 1019 Bytes
/
bw_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from PIL import Image, ImageStat
import os
def detect_color_image(file, thumb_size=40, MSE_cutoff=22, adjust_color_bias=True):
pil_img = Image.open(file)
bands = pil_img.getbands()
if bands == ('R','G','B') or bands== ('R','G','B','A'):
thumb = pil_img.resize((thumb_size,thumb_size))
SSE, bias = 0, [0,0,0]
if adjust_color_bias:
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)/(thumb_size*thumb_size)
if MSE <= MSE_cutoff:
#print ("grayscale\t")
return(0)
else:
#print ("Color\t\t\t")
return(1)
#print ("MSE = ",MSE)
elif len(bands)==1:
#print ("Black and white", bands)
return(0)
else:
#print ("Don't know...", bands)
return(1)