def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, as_base64=False, version=None): if zoom < ZOOM_MIN_LEVEL: zoom = ZOOM_MIN_LEVEL if zoom > ZOOM_MAX_LEVEL: zoom = ZOOM_MAX_LEVEL rotation = rotation % 360 file_path = self.get_valid_image(size=size, page=page, zoom=zoom, rotation=rotation, version=version) logger.debug('file_path: %s', file_path) if as_base64: mimetype = get_mimetype(open(file_path, 'r'), file_path, mimetype_only=True)[0] image = open(file_path, 'r') base64_data = base64.b64encode(image.read()) image.close() return 'data:%s;base64,%s' % (mimetype, base64_data) else: return file_path
def get_page_count(self, input_filepath): page_count = 1 mimetype, encoding = get_mimetype(open(input_filepath, 'rb'), input_filepath, mimetype_only=True) if mimetype == 'application/pdf': # If file is a PDF open it with slate to determine the page # count with open(input_filepath) as fd: try: pages = slate.PDF(fd) except: return 1 # TODO: Maybe return UnknownFileFormat to display proper unknwon file format message in document description return len(pages) try: im = Image.open(input_filepath) except IOError: # cannot identify image file raise UnknownFileFormat try: while 1: im.seek(im.tell() + 1) page_count += 1 # do something to im except EOFError: pass # end of sequence return page_count
def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, as_base64=False, version=None): if zoom < ZOOM_MIN_LEVEL: zoom = ZOOM_MIN_LEVEL if zoom > ZOOM_MAX_LEVEL: zoom = ZOOM_MAX_LEVEL rotation = rotation % 360 try: file_path = self.get_valid_image(size=size, page=page, zoom=zoom, rotation=rotation, version=version) except UnknownFileFormat: file_path = get_icon_file_path(self.file_mimetype) except UnkownConvertError: file_path = get_error_icon_file_path() except: file_path = get_error_icon_file_path() if as_base64: image = open(file_path, 'r') out = StringIO() base64.encode(image, out) return u'data:%s;base64,%s' % (get_mimetype(open(file_path, 'r'), file_path, mimetype_only=True)[0], out.getvalue().replace('\n', '')) else: return file_path
def convert(self, input_filepath, mimetype=None): self.exists = False self.mimetype = None self.encoding = None self.input_filepath = input_filepath # Make sure file is of a known office format if mimetype: self.mimetype = mimetype else: self.mimetype, self.encoding = get_mimetype( open(self.input_filepath), self.input_filepath, mimetype_only=True ) if self.mimetype in CONVERTER_OFFICE_FILE_MIMETYPES: # Cache results of conversion self.output_filepath = os.path.join( TEMPORARY_DIRECTORY, u"".join([self.input_filepath, CACHED_FILE_SUFFIX]) ) self.exists = os.path.exists(self.output_filepath) if not self.exists: try: self.backend.convert(self.input_filepath, self.output_filepath) self.exists = True except OfficeBackendError, msg: # convert exception so that at least the mime type icon is displayed raise UnknownFileFormat(msg)
def get_image(self, size, transformations): try: return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations) except UnknownFileFormat: return get_icon_file_path(get_mimetype(self.filepath)) except UnkownConvertError: return get_error_icon_file_path()
def get_page_count(self, input_filepath): page_count = 1 mimetype, encoding = get_mimetype(open(input_filepath, "rb"), input_filepath) if mimetype == "application/pdf": # If file is a PDF open it with slate to determine the page # count with open(input_filepath) as fd: pages = slate.PDF(fd) return len(pages) try: im = Image.open(input_filepath) except IOError: # cannot identify image file raise UnknownFileFormat try: while 1: im.seek(im.tell() + 1) page_count += 1 # do something to im except EOFError: pass # end of sequence return page_count
def get_page_count(self, input_filepath): page_count = 1 mimetype, encoding = get_mimetype(open(input_filepath, 'rb'), input_filepath, mimetype_only=True) if mimetype == 'application/pdf': # If file is a PDF open it with slate to determine the page # count with open(input_filepath) as fd: try: pages = slate.PDF(fd) except: return 1 # TODO: Maybe return UnknownFileFormat to display proper unknwon file format message in document description return len(pages) try: im = Image.open(input_filepath) except IOError: # cannot identify image file raise UnknownFileFormat try: while True: im.seek(im.tell() + 1) page_count += 1 # do something to im except EOFError: pass # end of sequence return page_count
def __init__(self, file_object, mime_type=None): self.file_object = file_object self.image = None self.mime_type = mime_type or get_mimetype( file_object=file_object, mimetype_only=False )[0] self.soffice_file = None
def convert(self, input_filepath, mimetype=None): self.exists = False self.mimetype = None self.encoding = None self.input_filepath = input_filepath # Make sure file is of a known office format if mimetype: self.mimetype = mimetype else: self.mimetype, self.encoding = get_mimetype(open( self.input_filepath), self.input_filepath, mimetype_only=True) if self.mimetype in CONVERTER_OFFICE_FILE_MIMETYPES: # Cache results of conversion self.output_filepath = os.path.join( TEMPORARY_DIRECTORY, ''.join([self.input_filepath, CACHED_FILE_SUFFIX])) self.exists = os.path.exists(self.output_filepath) if not self.exists: try: self.backend.convert(self.input_filepath, self.output_filepath) self.exists = True except OfficeBackendError as exception: # convert exception so that at least the mime type icon is displayed raise UnknownFileFormat(exception)
def convert_file(self, input_filepath, output_filepath, transformations=None, page=DEFAULT_PAGE_NUMBER, file_format=DEFAULT_FILE_FORMAT, **kwargs): tmpfile = None mimetype = kwargs.get('mimetype', None) if not mimetype: mimetype, encoding = get_mimetype(open(input_filepath, 'rb'), input_filepath, mimetype_only=True) try: if mimetype == 'application/pdf' and pdftoppm: image_buffer = io.BytesIO() pdftoppm(input_filepath, f=page, l=page, _out=image_buffer) image_buffer.seek(0) im = Image.open(image_buffer) else: im = Image.open(input_filepath) except Exception as exception: logger.error('Error converting image; %s', exception) # Python Imaging Library doesn't recognize it as an image raise ConvertError except IOError: # cannot identify image file raise UnknownFileFormat finally: if tmpfile: fs_cleanup(tmpfile) current_page = 0 try: while current_page == page - 1: im.seek(im.tell() + 1) current_page += 1 # do something to im except EOFError: # end of sequence pass try: if transformations: aspect = 1.0 * im.size[0] / im.size[1] for transformation in transformations: arguments = transformation.get('arguments') if transformation['transformation'] == TRANSFORMATION_RESIZE: width = int(arguments.get('width', 0)) height = int(arguments.get('height', 1.0 * width * aspect)) im = self.resize(im, (width, height)) elif transformation['transformation'] == TRANSFORMATION_ZOOM: decimal_value = float(arguments.get('percent', 100)) / 100 im = im.transform((int(im.size[0] * decimal_value), int(im.size[1] * decimal_value)), Image.EXTENT, (0, 0, im.size[0], im.size[1])) elif transformation['transformation'] == TRANSFORMATION_ROTATE: # PIL counter degress counter-clockwise, reverse them im = im.rotate(360 - arguments.get('degrees', 0)) except: # Ignore all transformation error pass if im.mode not in ('L', 'RGB'): im = im.convert('RGB') im.save(output_filepath, format=file_format)
def get_image(self, size, transformations): try: return self.get_valid_image(size=size, transformations=transformations) #return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations) except UnknownFileFormat: mimetype, encoding = get_mimetype(open(self.filepath, 'rb'), self.filepath) return get_icon_file_path(mimetype) except UnkownConvertError: return get_error_icon_file_path()
def open(cls, file_object): mime_type = get_mimetype(file_object=file_object, mimetype_only=True)[0] try: for archive_class in cls._registry[mime_type]: instance = archive_class() instance._open(file_object=file_object) return instance except KeyError: raise NoMIMETypeMatch
def open(cls, file_object): mime_type = get_mimetype( file_object=file_object, mimetype_only=True )[0] try: for archive_class in cls._registry[mime_type]: instance = archive_class() instance._open(file_object=file_object) return instance except KeyError: raise NoMIMETypeMatch
def get_image(self, size, page, zoom, rotation, as_base64=True): # TODO: add support for transformations converted_file_path = convert(self.get_full_path(), size=size) if as_base64: mimetype = get_mimetype(open(converted_file_path, 'r'), converted_file_path, mimetype_only=True)[0] image = open(converted_file_path, 'r') base64_data = base64.b64encode(image.read()) image.close() return u'data:%s;base64,%s' % (mimetype, base64_data) else: return converted_file_path
def update_mimetype(self, save=True): """ Read a document verions's file and determine the mimetype by calling the get_mimetype wrapper """ if self.exists(): try: self.mimetype, self.encoding = get_mimetype(self.open(), self.filename) except: self.mimetype = u'' self.encoding = u'' finally: if save: self.save()
def update_mimetype(self, save=True): """ Read a document verions's file and determine the mimetype by calling the get_mimetype wrapper """ if self.exists(): try: with self.open() as file_object: self.mimetype, self.encoding = get_mimetype( file_object=file_object) except Exception: self.mimetype = '' self.encoding = '' finally: if save: self.save()
def update_mimetype(self, save=True): """ Read a document verions's file and determine the mimetype by calling the get_mimetype wrapper """ if self.exists(): try: with self.open() as file_object: self.mimetype, self.encoding = get_mimetype( file_object=file_object ) except Exception: self.mimetype = '' self.encoding = '' finally: if save: self.save()
def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, as_base64=False, version=None): if zoom < ZOOM_MIN_LEVEL: zoom = ZOOM_MIN_LEVEL if zoom > ZOOM_MAX_LEVEL: zoom = ZOOM_MAX_LEVEL rotation = rotation % 360 file_path = self.get_valid_image(size=size, page=page, zoom=zoom, rotation=rotation, version=version) logger.debug('file_path: %s' % file_path) if as_base64: mimetype = get_mimetype(open(file_path, 'r'), file_path, mimetype_only=True)[0] image = open(file_path, 'r') base64_data = base64.b64encode(image.read()) image.close() return u'data:%s;base64,%s' % (mimetype, base64_data) else: return file_path
def get_image(self, size=DISPLAY_SIZE, page=DEFAULT_PAGE_NUMBER, zoom=DEFAULT_ZOOM_LEVEL, rotation=DEFAULT_ROTATION, as_base64=False, version=None): if zoom < ZOOM_MIN_LEVEL: zoom = ZOOM_MIN_LEVEL if zoom > ZOOM_MAX_LEVEL: zoom = ZOOM_MAX_LEVEL rotation = rotation % 360 try: file_path = self.get_valid_image(size=size, page=page, zoom=zoom, rotation=rotation, version=version) except UnknownFileFormat: file_path = get_icon_file_path(self.file_mimetype) except UnkownConvertError: file_path = get_error_icon_file_path() except: file_path = get_error_icon_file_path() if as_base64: image = open(file_path, 'r') out = StringIO() base64.encode(image, out) return u'data:%s;base64,%s' % (get_mimetype( open(file_path, 'r'), file_path, mimetype_only=True)[0], out.getvalue().replace('\n', '')) else: return file_path
def update_mimetype(self, save=True): """ Read a file and determine the mimetype by calling the get_mimetype wrapper """ if self.exists(): try: source = open(self.file.path, 'rb') mimetype, mime_encoding = get_mimetype(source, self.file_filename) if mimetype: self.file_mimetype = mimetype else: self.file_mimetype = u'' if mime_encoding: self.file_mime_encoding = mime_encoding else: self.file_mime_encoding = u'' except: self.file_mimetype = u'' self.file_mime_encoding = u'' finally: if save: self.save()
def convert_file( self, input_filepath, output_filepath, transformations=None, page=DEFAULT_PAGE_NUMBER, file_format=DEFAULT_FILE_FORMAT, ): tmpfile = None mimetype, encoding = get_mimetype(open(input_filepath, "rb"), input_filepath) if mimetype == "application/pdf" and USE_GHOSTSCRIPT: # If file is a PDF open it with ghostscript and convert it to # TIFF first_page_tmpl = "-dFirstPage=%d" % page last_page_tmpl = "-dLastPage=%d" % page fd, tmpfile = tempfile.mkstemp() os.close(fd) output_file_tmpl = "-sOutputFile=%s" % tmpfile input_file_tmpl = "-f%s" % input_filepath args = [ "gs", "-q", "-dQUIET", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-dNOPROMPT", first_page_tmpl, last_page_tmpl, "-sDEVICE=jpeg", "-dJPEGQ=75", "-r150", output_file_tmpl, input_file_tmpl, '-c "60000000 setvmthreshold"', # use 30MB "-dNOGC", # No garbage collection "-dMaxBitmap=500000000", "-dAlignToPixels=0", "-dGridFitTT=0", "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", ] ghostscript.Ghostscript(*args) page = 1 # Don't execute the following while loop input_filepath = tmpfile try: im = Image.open(input_filepath) except Exception: # Python Imaging Library doesn't recognize it as an image raise UnknownFileFormat finally: if tmpfile: cleanup(tmpfile) current_page = 0 try: while current_page == page - 1: im.seek(im.tell() + 1) current_page += 1 # do something to im except EOFError: # end of sequence pass try: if transformations: aspect = 1.0 * im.size[0] / im.size[1] for transformation in transformations: arguments = transformation.get("arguments") if transformation["transformation"] == TRANSFORMATION_RESIZE: width = int(arguments.get("width", 0)) height = int(arguments.get("height", 1.0 * width * aspect)) im = self.resize(im, (width, height)) elif transformation["transformation"] == TRANSFORMATION_ZOOM: decimal_value = float(arguments.get("percent", 100)) / 100 im = im.transform( (int(im.size[0] * decimal_value), int(im.size[1] * decimal_value)), Image.EXTENT, (0, 0, im.size[0], im.size[1]), ) elif transformation["transformation"] == TRANSFORMATION_ROTATE: # PIL counter degress counter-clockwise, reverse them im = im.rotate(360 - arguments.get("degrees", 0)) except: # Ignore all transformation error pass if im.mode not in ("L", "RGB"): im = im.convert("RGB") im.save(output_filepath, format=file_format)
def convert_file(self, input_filepath, output_filepath, transformations=None, page=DEFAULT_PAGE_NUMBER, file_format=DEFAULT_FILE_FORMAT, **kwargs): tmpfile = None mimetype = kwargs.get('mimetype', None) if not mimetype: mimetype, encoding = get_mimetype(open(input_filepath, 'rb'), input_filepath, mimetype_only=True) if mimetype == 'application/pdf' and USE_GHOSTSCRIPT: # If file is a PDF open it with ghostscript and convert it to # TIFF first_page_tmpl = '-dFirstPage=%d' % page last_page_tmpl = '-dLastPage=%d' % page fd, tmpfile = tempfile.mkstemp() os.close(fd) output_file_tmpl = '-sOutputFile=%s' % tmpfile input_file_tmpl = '-f%s' % input_filepath args = [ 'gs', '-q', '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT', first_page_tmpl, last_page_tmpl, '-sDEVICE=jpeg', '-dJPEGQ=95', '-r150', output_file_tmpl, input_file_tmpl, '-c "60000000 setvmthreshold"', # use 30MB '-dNOGC', # No garbage collection '-dMaxBitmap=500000000', '-dAlignToPixels=0', '-dGridFitTT=0', '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', ] ghostscript.Ghostscript(*args) page = 1 # Don't execute the following while loop input_filepath = tmpfile try: im = Image.open(input_filepath) except Exception: # Python Imaging Library doesn't recognize it as an image raise UnknownFileFormat finally: if tmpfile: cleanup(tmpfile) current_page = 0 try: while current_page == page - 1: im.seek(im.tell() + 1) current_page += 1 # do something to im except EOFError: # end of sequence pass try: if transformations: aspect = 1.0 * im.size[0] / im.size[1] for transformation in transformations: arguments = transformation.get('arguments') if transformation['transformation'] == TRANSFORMATION_RESIZE: width = int(arguments.get('width', 0)) height = int(arguments.get('height', 1.0 * width * aspect)) im = self.resize(im, (width, height)) elif transformation['transformation'] == TRANSFORMATION_ZOOM: decimal_value = float(arguments.get('percent', 100)) / 100 im = im.transform((int(im.size[0] * decimal_value), int(im.size[1] * decimal_value)), Image.EXTENT, (0, 0, im.size[0], im.size[1])) elif transformation['transformation'] == TRANSFORMATION_ROTATE: # PIL counter degress counter-clockwise, reverse them im = im.rotate(360 - arguments.get('degrees', 0)) except: # Ignore all transformation error pass if im.mode not in ('L', 'RGB'): im = im.convert('RGB') im.save(output_filepath, format=file_format)
def convert_file(self, input_filepath, output_filepath, transformations=None, page=DEFAULT_PAGE_NUMBER, file_format=DEFAULT_FILE_FORMAT, **kwargs): tmpfile = None mimetype = kwargs.get('mimetype', None) if not mimetype: mimetype, encoding = get_mimetype(open(input_filepath, 'rb'), input_filepath, mimetype_only=True) if mimetype == 'application/pdf' and USE_GHOSTSCRIPT: # If file is a PDF open it with ghostscript and convert it to # TIFF first_page_tmpl = '-dFirstPage=%d' % page last_page_tmpl = '-dLastPage=%d' % page fd, tmpfile = tempfile.mkstemp() os.close(fd) output_file_tmpl = '-sOutputFile=%s' % tmpfile input_file_tmpl = '-f%s' % input_filepath args = [ 'gs', '-q', '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT', first_page_tmpl, last_page_tmpl, '-sDEVICE=jpeg', '-dJPEGQ=95', '-r150', output_file_tmpl, input_file_tmpl, '-c "60000000 setvmthreshold"', # use 30MB '-dNOGC', # No garbage collection '-dMaxBitmap=500000000', '-dAlignToPixels=0', '-dGridFitTT=0', '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', ] ghostscript.Ghostscript(*args) page = 1 # Don't execute the following while loop input_filepath = tmpfile try: im = Image.open(input_filepath) except Exception: # Python Imaging Library doesn't recognize it as an image raise UnknownFileFormat finally: if tmpfile: cleanup(tmpfile) current_page = 0 try: while current_page == page - 1: im.seek(im.tell() + 1) current_page += 1 # do something to im except EOFError: # end of sequence pass try: if transformations: aspect = 1.0 * im.size[0] / im.size[1] for transformation in transformations: arguments = transformation.get('arguments') if transformation[ 'transformation'] == TRANSFORMATION_RESIZE: width = int(arguments.get('width', 0)) height = int( arguments.get('height', 1.0 * width * aspect)) im = self.resize(im, (width, height)) elif transformation[ 'transformation'] == TRANSFORMATION_ZOOM: decimal_value = float(arguments.get('percent', 100)) / 100 im = im.transform((int(im.size[0] * decimal_value), int(im.size[1] * decimal_value)), Image.EXTENT, (0, 0, im.size[0], im.size[1])) elif transformation[ 'transformation'] == TRANSFORMATION_ROTATE: # PIL counter degress counter-clockwise, reverse them im = im.rotate(360 - arguments.get('degrees', 0)) except: # Ignore all transformation error pass if im.mode not in ('L', 'RGB'): im = im.convert('RGB') im.save(output_filepath, format=file_format)