text: ###################################################
  ##This file include functions to evaluate quality of images
  # images are imported as PIL object
  # external package needed: scikit-image
  # reference:
  # kaggle notebooks: https://www.kaggle.com/shivamb/ideas-for-image-features-and-image-quality/notebook
  
  
  # Zehai Wang 2018 May 31
  
  ####################################################
  
  from collections import defaultdict
  from scipy.stats import itemfreq
  from scipy import ndimage as ndi
  import matplotlib.pyplot as plt
  from skimage import feature
  from PIL import Image as IMG
  import numpy as np
  import pandas as pd 
  import operator
  import cv2
  import os
  
  from IPython.core.display import HTML 
  from IPython.display import Image
  
  
  # Dullness and whiteness score (color range and impression to the viewers)
  def color_analysis(img):
      # obtain the color palatte of the image 
      palatte = defaultdict(int)
      for pixel in img.getdata():
          palatte[pixel] += 1
      
      # sort the colors present in the image 
      sorted_x = sorted(palatte.items(), key=operator.itemgetter(1), reverse = True)
      light_shade, dark_shade, shade_count, pixel_limit = 0, 0, 0, 25
      for i, x in enumerate(sorted_x[:pixel_limit]):
          if all(xx <= 20 for xx in x[0][:3]): ## dull : too much darkness 
              dark_shade += x[1]
          if all(xx >= 240 for xx in x[0][:3]): ## bright : too much whiteness 
              light_shade += x[1]
          shade_count += x[1]
          
      light_percent = round((float(light_shade)/shade_count)*100, 2)
      dark_percent = round((float(dark_shade)/shade_count)*100, 2)
      return light_percent, dark_percent
  
  def perform_color_analysis(img, flag, images_path):
      path = images_path + img 
      im = IMG.open(path) #.convert("RGB")
      
      # cut the images into two halves as complete average may give bias results
      size = im.size
      halves = (size[0]/2, size[1]/2)
      im1 = im.crop((0, 0, size[0], halves[1]))
      im2 = im.crop((0, halves[1], size[0], size[1]))
  
      try:
          light_percent1, dark_percent1 = color_analysis(im1)
          light_percent2, dark_percent2 = color_analysis(im2)
      except Exception as e:
          return None
  
      light_percent = (light_percent1 + light_percent2)/2 
      dark_percent = (dark_percent1 + dark_percent2)/2 
      
      if flag == 'black':
          return dark_percent
      elif flag == 'white':
          return light_percent
      else:
          return None
  
  
  
  
  if __name__ == "__main__":
      
      images_path = '../../input/avito_image/'
      imgs = os.listdir(images_path)
  
      features = pd.DataFrame()
      features['image'] = imgs
  
    