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)
def __init__(self, mongo_id, file_name=None): # Results storage. self.results = 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 = 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, 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 = AutoVivification()
def __init__(self, file_data): self.file_data = file_data self.results = AutoVivification()
def __init__(self, file_path): self.file_path = file_path self.results = AutoVivification()
def __init__(self): # Results storage. self.results = AutoVivification() # Data (results) in the global dictionary (produced by other modules). self.data = None