Advertisement
Guest User

Untitled

a guest
Jun 15th, 2021
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. def detect_color_image(file, thumb_size=40, MSE_cutoff=200, adjust_color_bias=True):
  2.     pil_img = Image.open(file)
  3.     bands = pil_img.getbands()
  4.     if bands == ('R','G','B') or bands== ('R','G','B','A'):
  5.         thumb = pil_img.resize((thumb_size,thumb_size))
  6.         SSE, bias = 0, [0,0,0]
  7.         if adjust_color_bias:
  8.             bias = ImageStat.Stat(thumb).mean[:3]
  9.             bias = [b - sum(bias)/3 for b in bias ]
  10.         for pixel in thumb.getdata():
  11.             mu = sum(pixel)/3
  12.             SSE += sum((pixel[i] - mu - bias[i])*(pixel[i] - mu - bias[i]) for i in [0,1,2])
  13.         MSE = float(SSE)/(thumb_size*thumb_size)
  14.         if MSE <= MSE_cutoff:
  15.             print ("Grayscale - Moving")
  16.             shutil.move(file_directory, "D:\lbpcascade_animeface-master\gray256")
  17.  
  18.         else:
  19.             print ("Color\t\t\t"),
  20.         print ("( MSE=",MSE,")")
  21.     elif len(bands)==1:
  22.         print ("Black and White"), bands
  23.     else:
  24.         print ("Unknown"), bands
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement