def calculate_difference(preview, original_image_id): """Calculate difference between two images. @param preview: preview dict @param original_image_id: original image ID @return: difference, difference percentage """ try: i1 = utils.str2image(get_file(original_image_id).read()) except IOError as e: logger.warning("Comparer error reading image: {0}".format(e)) return # Check if thumb was resized. if "original_file" in preview: i2 = utils.str2image(get_file(preview["original_file"]).read()) else: i2 = utils.str2image(get_file(preview["file"]).read()) # Resize. width, height = i2.size try: i1 = i1.resize([width, height], Image.ANTIALIAS) except IOError as e: logger.warning("Comparer error reading image: {0}".format(e)) return # Checks. #assert i1.mode == i2.mode, "Different kinds of images." #assert i1.size == i2.size, "Different sizes." # Calculate difference. pairs = izip(i1.getdata(), i2.getdata()) if len(i1.getbands()) == 1: # for gray-scale jpegs dif = sum(abs(p1 - p2) for p1, p2 in pairs) else: dif = sum( abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2)) ncomponents = i1.size[0] * i1.size[1] * 3 # Get diff percentage. diff_perc = int((dif / 255.0 * 100) / ncomponents) # Binary option. if diff_perc >= 15: diff = True else: diff = False return diff, diff_perc
def calculate_difference(preview, original_image_id): """Calculate difference between two images. @param preview: preview dict @param original_image_id: original image ID @return: difference, difference percentage """ try: i1 = utils.str2image(get_file(original_image_id).read()) except IOError as e: logger.warning("Comparer error reading image: {0}".format(e)) return # Check if thumb was resized. if "original_file" in preview: i2 = utils.str2image(get_file(preview["original_file"]).read()) else: i2 = utils.str2image(get_file(preview["file"]).read()) # Resize. width, height = i2.size try: i1 = i1.resize([width, height], Image.ANTIALIAS) except IOError as e: logger.warning("Comparer error reading image: {0}".format(e)) return # Checks. # assert i1.mode == i2.mode, "Different kinds of images." # assert i1.size == i2.size, "Different sizes." # Calculate difference. pairs = izip(i1.getdata(), i2.getdata()) if len(i1.getbands()) == 1: # for gray-scale jpegs dif = sum(abs(p1 - p2) for p1, p2 in pairs) else: dif = sum(abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2)) ncomponents = i1.size[0] * i1.size[1] * 3 # Get diff percentage. diff_perc = int((dif / 255.0 * 100) / ncomponents) # Binary option. if diff_perc >= 15: diff = True else: diff = False return diff, diff_perc
def _get_previews(self): """Extract previews.""" if len(self.metadata.get_preview_properties()) > 0: # Fetched previews key. self.results["preview"] = [] for preview in self.metadata.get_preview_properties(): p = AutoVivification() p["mime_type"] = preview.get_mime_type() p["size"] = len(self.metadata.get_preview_image(preview).get_data()) p["ext"] = preview.get_extension() p["dimensions"] = [preview.get_width(), preview.get_height()] # Resize if needed, and store. try: img = utils.str2image(self.metadata.get_preview_image(preview).get_data()) if preview.get_width() > 256 or preview.get_height() > 160: p["original_file"] = save_file(utils.image2str(img), content_type="image/jpeg") img.thumbnail([256, 160], Image.ANTIALIAS) p["file"] = save_file(utils.image2str(img), content_type="image/jpeg") except Exception as e: logger.warning("Error reading preview: {0}".format(e)) continue finally: # Save. self.results["preview"].append(p)
def _get_previews(self): """Extract previews.""" if len(self.metadata.get_preview_properties()) > 0: # Fetched previews key. self.results["preview"] = [] for preview in self.metadata.get_preview_properties(): p = AutoVivification() p["mime_type"] = preview.get_mime_type() p["size"] = len( self.metadata.get_preview_image(preview).get_data()) p["ext"] = preview.get_extension() p["dimensions"] = [preview.get_width(), preview.get_height()] # Resize if needed, and store. try: img = utils.str2image( self.metadata.get_preview_image(preview).get_data()) if preview.get_width() > 256 or preview.get_height() > 160: p["original_file"] = save_file( utils.image2str(img), content_type="image/jpeg") img.thumbnail([256, 160], Image.ANTIALIAS) p["file"] = save_file(utils.image2str(img), content_type="image/jpeg") except Exception as e: logger.warning("Error reading preview: {0}".format(e)) continue finally: # Save. self.results["preview"].append(p)