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(db.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(db.get_file(preview["original_file"]).read()) else: i2 = utils.str2image(db.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(db.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(db.get_file(preview["original_file"]).read()) else: i2 = utils.str2image(db.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 __init__(self, mongo_id, file_name=None): # Results storage. self.results = utils.AutoVivification() # Store image id. if mongo_id: self.orig_id = mongo_id else: raise Exception("You have to pass the original image ID") if file_name: self.file_name = file_name else: self.file_name = None # Read image data. try: self.file_data = db.get_file(self.orig_id).read() except gridfs.errors.NoFile: raise Exception("Image not found on GridFS storage") # Save a temporary file, used for analysis which needs a file on disk. temp_image = tempfile.NamedTemporaryFile(delete=False) temp_image.write(self.file_data) temp_image.close() # File is not immediately deleted because we used delete=False self.temp_image = temp_image.name
def __init__(self, mongo_id, file_name=None): # Results storage. self.results = utils.AutoVivification() # Store image id. if mongo_id: self.orig_id = mongo_id else: raise Exception("You have to pass the original image ID") if file_name: self.file_name = file_name else: self.file_name = None # Read image data. try: self.file_data = db.get_file(self.orig_id).read() except gridfs.errors.NoFile: raise Exception("Image not found on GridFS storage") # Save a temporary file, used for analysis which needs a file on disk. temp_image = tempfile.NamedTemporaryFile(delete=False) temp_image.write(self.file_data) temp_image.close( ) # File is not immediately deleted because we used delete=False self.temp_image = temp_image.name
def image(request, id): try: file = get_file(id) except (InvalidId, TypeError): return render_to_response("error.html", {"error": "Unable to load image."}, context_instance=RequestContext(request)) data = file.read() response = HttpResponse(data, content_type=file.content_type) response["Content-Disposition"] = "attachment; filename=%s" % id response["Content-Length"] = len(data) return response