old_contents: from PIL import Image
  
  
  def _hamming_distance(string, other_string):
      """ Computes the hamming distance between two strings. """
      return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(string, other_string)))
  
  
  def average_hash(image_path, hash_size=8):
      """ Computes the average hash of the given image. """
  
      # Open the image, resize it and convert it to black & white.
      image = Image.open(image_path)
      image = image.resize((hash_size, hash_size), Image.ANTIALIAS).convert('L')
  
      # Get the average value of a pixel in the image.
      pixels = list(image.getdata())
      avg = sum(pixels) / len(pixels)
  
      # Compute the hash based on each pixels value compared to the average.
      bits = "".join(map(lambda pixel: '1' if pixel > avg else '0', pixels))
      return int(bits, 2).__format__('016x')
  
  
  def distance(image_path, other_image_path):
      """ Computes the hamming distance between two images. """
      image_hash = average_hash(image_path)
      other_image_hash = average_hash(other_image_path)
  
      return _hamming_distance(image_hash, other_image_hash)
  
  
  def is_look_alike(image_path, other_image_path, tolerance=6):
      """
      Returns True if the hamming distance between
      the image hashes are less than the given tolerance.
      """
  
      return distance(image_path, other_image_path) <= tolerance
  
new_contents: from PIL import Image
  
  
  def _hamming_distance(string, other_string):
      """ Computes the hamming distance between two strings. """
      return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(string, other_string)))
  
  
  def average_hash(image_path, hash_size=8):
      """ Computes the average hash of the given image. """
  
      # Open the image, resize it and convert it to black & white.
      image = Image.open(image_path)
      image = image.resize((hash_size, hash_size), Image.ANTIALIAS).convert('L')
  
      # Get the average value of a pixel in the image.
      pixels = list(image.getdata())
      avg = sum(pixels) / len(pixels)
  
      # Compute the hash based on each pixels value compared to the average.
      bits = "".join(map(lambda pixel: '1' if pixel > avg else '0', pixels))
      hashformat = "0{hashlength}x".format(hashlength=hash_size * 2)
      return int(bits, 2).__format__(hashformat)
  
  
  def distance(image_path, other_image_path):
      """ Computes the hamming distance between two images. """
      image_hash = average_hash(image_path)
      other_image_hash = average_hash(other_image_path)
  
      return _hamming_distance(image_hash, other_image_hash)
  
  
  def is_look_alike(image_path, other_image_path, tolerance=6):
      """
      Returns True if the hamming distance between
      the image hashes are less than the given tolerance.
      """
  
      return distance(image_path, other_image_path) <= tolerance
  
subject: Make hash output vary on hash_size.