def run(self, task): # NOTE: used to allow processing of malformed images. # With default settings they raise errors like "IOError: image file is truncated (0 bytes not processed)". # See: http://stackoverflow.com/questions/9211719/python-pil-image-error-after-image-load ImageFile.LOAD_TRUNCATED_IMAGES = True # Check this kind of analysis should be run on this file type. # It can be applied only on PNG and JPEG formats. if "mime_type" in self.data and not (self.data["mime_type"] == "image/png" or self.data["mime_type"] == "image/jpeg"): return self.results # Create temporary file. handle, resaved = tempfile.mkstemp() tmp_file = str2temp_file(task.get_file_data) # Open file and resave it. try: im = Image.open(tmp_file.name) im.save(resaved, "JPEG", quality=95) resaved_im = Image.open(resaved) except IOError as e: logger.warning("[Task {0}]: ELA error opening image: {1}".format(task.id, e)) return self.results finally: tmp_file.close() os.close(handle) os.remove(resaved) # Trick to convert images like PNG to a format comparable with JPEG. if im.mode != "RGB": im = im.convert("RGB") # Create ELA image. try: ela_im = ImageChops.difference(im, resaved_im) except Exception as e: logger.warning("[Task {0}]: Unable to calculate ELA difference: {0}".format(task.id, e)) return self.results # Calculate difference extrema = ela_im.getextrema() max_diff = max([ex[1] for ex in extrema]) if not max_diff: return self.results scale = 255.0/max_diff ela_im = ImageEnhance.Brightness(ela_im).enhance(scale) self.results["ela"]["max_difference"] = max([ex[1] for ex in extrema]) # Resize image if it's too big. width, height = ela_im.size if width > 1800: ela_im.thumbnail([1800, 1800], Image.ANTIALIAS) # Save image. try: img = image2str(ela_im) self.results["ela"]["ela_image"] = save_file(img, content_type="image/jpeg") except Exception as e: logger.warning("[Task {0}]: ELA error saving image: {0}".format(task.id, e)) finally: return self.results
def run(self, task): # Save task id for logging. self.task_id = task.id # Read metadata from a temp file. try: tmp_file = str2temp_file(task.get_file_data) self.metadata = GExiv2.Metadata() self.metadata.open_path(str(tmp_file.name)) except Exception as e: logger.warning("[Task {0}]: Unable to read image metadata: {1}".format(task.id, e)) self.metadata = None finally: tmp_file.close() # Run all analysis. if self.metadata: self._get_comment() self._get_dimensions() self._get_exif() self._get_iptc() self._get_xmp() self._get_previews() self._get_gps_data() return self.results
def run(self, task): # Read metadata from a temp file. try: tmp_file = str2temp_file(task.get_file_data) self.metadata = GExiv2.Metadata() self.metadata.open_path(str(tmp_file.name)) except Exception as e: logger.warning("Unable to read image metadata: {0}".format(e)) self.metadata = None finally: tmp_file.close() # Run all analysis. if self.metadata: self._get_comment() self._get_dimensions() self._get_exif() self._get_iptc() self._get_xmp() self._get_previews() self._get_gps_data() return self.results