Ejemplo n.º 1
0
def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, **kwargs):
    size = kwargs.get('size')
    file_format = kwargs.get('file_format', DEFAULT_FILE_FORMAT)
    zoom = kwargs.get('zoom', DEFAULT_ZOOM_LEVEL)
    rotation = kwargs.get('rotation', DEFAULT_ROTATION)
    page = kwargs.get('page', DEFAULT_PAGE_NUMBER)
    transformations = kwargs.get('transformations', [])
    if transformations is None:
        transformations = []

    unoconv_output = None

    if output_filepath is None:
        output_filepath = create_image_cache_filename(input_filepath, *args, **kwargs)
        
    if os.path.exists(output_filepath):
        return output_filepath

    path, extension = os.path.splitext(input_filepath)
    if extension[1:].lower() in CONVERTER_OFFICE_FILE_EXTENSIONS:
        result = convert_office_document(input_filepath)
        if result:
            unoconv_output = result
            input_filepath = result

    if size:
        transformations.append(
            {
                'transformation': TRANSFORMATION_RESIZE,
                'arguments': dict(zip([u'width', u'height'], size.split(DIMENSION_SEPARATOR)))
            }
        )

    if zoom != 100:
        transformations.append(
            {
                'transformation': TRANSFORMATION_ZOOM,
                'arguments': {'percent': zoom}
            }
        )        

    if rotation != 0 and rotation != 360:
        transformations.append(
            {
                'transformation': TRANSFORMATION_ROTATE,
                'arguments': {'degrees': rotation}
            }
        )           

    try:
        backend.convert_file(input_filepath=input_filepath, output_filepath=output_filepath, transformations=transformations, page=page, file_format=file_format)
    finally:
        if cleanup_files:
            cleanup(input_filepath)
        if unoconv_output:
            cleanup(unoconv_output)

    return output_filepath
Ejemplo n.º 2
0
def convert(input_filepath, output_filepath=None, cleanup_files=False, mimetype=None, *args, **kwargs):
    size = kwargs.get('size')
    file_format = kwargs.get('file_format', DEFAULT_FILE_FORMAT)
    zoom = kwargs.get('zoom', DEFAULT_ZOOM_LEVEL)
    rotation = kwargs.get('rotation', DEFAULT_ROTATION)
    page = kwargs.get('page', DEFAULT_PAGE_NUMBER)
    transformations = kwargs.get('transformations', [])
    
    if transformations is None:
        transformations = []

    if output_filepath is None:
        output_filepath = create_image_cache_filename(input_filepath, *args, **kwargs)
        
    if os.path.exists(output_filepath):
        return output_filepath

    if office_converter:
        try:
            office_converter.convert(input_filepath, mimetype=mimetype)
            if office_converter.exists:
                input_filepath = office_converter.output_filepath
                mimetype = 'application/pdf'
            else:
                # Recycle the already detected mimetype
                mimetype = office_converter.mimetype

        except OfficeConversionError:
                raise UnknownFileFormat('office converter exception')

    if size:
        transformations.append(
            {
                'transformation': TRANSFORMATION_RESIZE,
                'arguments': dict(zip([u'width', u'height'], size.split(DIMENSION_SEPARATOR)))
            }
        )

    if zoom != 100:
        transformations.append(
            {
                'transformation': TRANSFORMATION_ZOOM,
                'arguments': {'percent': zoom}
            }
        )        

    if rotation != 0 and rotation != 360:
        transformations.append(
            {
                'transformation': TRANSFORMATION_ROTATE,
                'arguments': {'degrees': rotation}
            }
        )           

    try:
        backend.convert_file(input_filepath=input_filepath, output_filepath=output_filepath, transformations=transformations, page=page, file_format=file_format, mimetype=mimetype)
    finally:
        if cleanup_files:
            cleanup(input_filepath)

    return output_filepath
Ejemplo n.º 3
0
Archivo: base.py Proyecto: x3n0/mayan
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
def convert(input_filepath,
            output_filepath=None,
            cleanup_files=False,
            mimetype=None,
            *args,
            **kwargs):
    size = kwargs.get('size')
    file_format = kwargs.get('file_format', DEFAULT_FILE_FORMAT)
    zoom = kwargs.get('zoom', DEFAULT_ZOOM_LEVEL)
    rotation = kwargs.get('rotation', DEFAULT_ROTATION)
    page = kwargs.get('page', DEFAULT_PAGE_NUMBER)
    transformations = kwargs.get('transformations', [])

    if transformations is None:
        transformations = []

    if output_filepath is None:
        output_filepath = create_image_cache_filename(input_filepath, *args,
                                                      **kwargs)

    if os.path.exists(output_filepath):
        return output_filepath

    if office_converter:
        try:
            office_converter.convert(input_filepath, mimetype=mimetype)
            if office_converter.exists:
                input_filepath = office_converter.output_filepath
                mimetype = 'application/pdf'
            else:
                # Recycle the already detected mimetype
                mimetype = office_converter.mimetype

        except OfficeConversionError:
            raise UnknownFileFormat('office converter exception')

    if size:
        transformations.append({
            'transformation':
            TRANSFORMATION_RESIZE,
            'arguments':
            dict(zip([u'width', u'height'], size.split(DIMENSION_SEPARATOR)))
        })

    if zoom != 100:
        transformations.append({
            'transformation': TRANSFORMATION_ZOOM,
            'arguments': {
                'percent': zoom
            }
        })

    if rotation != 0 and rotation != 360:
        transformations.append({
            'transformation': TRANSFORMATION_ROTATE,
            'arguments': {
                'degrees': rotation
            }
        })

    try:
        backend.convert_file(input_filepath=input_filepath,
                             output_filepath=output_filepath,
                             transformations=transformations,
                             page=page,
                             file_format=file_format,
                             mimetype=mimetype)
    finally:
        if cleanup_files:
            cleanup(input_filepath)

    return output_filepath