Ejemplo n.º 1
0
    def _get_previews(self):
        """Extract previews."""
        if len(self.metadata.get_preview_properties()) > 0:
            # Fetched previews key.
            self.results["metadata"]["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 = str2image(self.metadata.get_preview_image(preview).get_data())
                    if preview.get_width() > 256 or preview.get_height() > 160:
                        p["original_file"] = save_file(image2str(img), content_type="image/jpeg")
                    img.thumbnail([256, 160], Image.ANTIALIAS)
                    p["file"] = save_file(image2str(img), content_type="image/jpeg")
                except Exception as e:
                    logger.warning("Error reading preview: {0}".format(e))
                    continue
                finally:
                    # Save.
                    self.results["metadata"]["preview"].append(p)
Ejemplo n.º 2
0
    def _get_previews(self):
        """Extract previews."""
        if len(self.metadata.get_preview_properties()) > 0:
            # Fetched previews key.
            self.results["metadata"]["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 = str2image(self.metadata.get_preview_image(preview).get_data())
                    if preview.get_width() > 256 or preview.get_height() > 160:
                        p["original_file"] = save_file(image2str(img), content_type="image/jpeg")
                    img.thumbnail([256, 160], Image.ANTIALIAS)
                    p["file"] = save_file(image2str(img), content_type="image/jpeg")
                except Exception as e:
                    logger.warning("Error reading preview: {0}".format(e))
                    continue
                finally:
                    # Save.
                    self.results["metadata"]["preview"].append(p)
Ejemplo n.º 3
0
    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