Пример #1
0
    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 = utils.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"] = db.save_file(
                            utils.image2str(img), content_type="image/jpeg")
                    img.thumbnail([256, 160], Image.ANTIALIAS)
                    p["file"] = db.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)
Пример #2
0
    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
Пример #3
0
    def __init__(self, file_path):
        try:
            self.metadata = GExiv2.Metadata(file_path)
        except Exception as e:
            logger.warning("Unable to read image metadata: {0}".format(e))
            self.metadata = None

        self.results = utils.AutoVivification()
Пример #4
0
 def __init__(self, file_data):
     self.file_data = file_data
     self.results = utils.AutoVivification()
Пример #5
0
 def __init__(self, file_path):
     self.file_path = file_path
     self.results = utils.AutoVivification()