Python reportlab.lib.utils.ImageReader() Examples

The following are 16 code examples of reportlab.lib.utils.ImageReader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module reportlab.lib.utils , or try the search function .
Example #1
Source File: paraparser.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def end_img(self):
        frag = self._stack[-1]
        if not getattr(frag,'_selfClosingTag',''):
            raise ValueError('Parser failure in <img/>')
        defn = frag.cbDefn = ABag()
        defn.kind = 'img'
        defn.src = getattr(frag,'src',None)
        defn.image = ImageReader(defn.src)
        size = defn.image.getSize()
        defn.width = getattr(frag,'width',size[0])
        defn.height = getattr(frag,'height',size[1])
        defn.valign = getattr(frag,'valign','bottom')
        del frag._selfClosingTag
        self.handle_data('')
        self._pop('img')

    #### super script 
Example #2
Source File: pdfutils.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def makeA85Image(filename,IMG=None):
    import zlib
    img = ImageReader(filename)
    if IMG is not None: IMG.append(img)

    imgwidth, imgheight = img.getSize()
    raw = img.getRGBData()

    code = []
    append = code.append
    # this describes what is in the image itself
    append('BI')
    append('/W %s /H %s /BPC 8 /CS /%s /F [/A85 /Fl]' % (imgwidth, imgheight,_mode2cs[img.mode]))
    append('ID')
    #use a flate filter and Ascii Base 85
    assert len(raw) == imgwidth * imgheight*_mode2bpp[img.mode], "Wrong amount of data for image"
    compressed = zlib.compress(raw)   #this bit is very fast...
    encoded = asciiBase85Encode(compressed) #...sadly this may not be

    #append in blocks of 60 characters
    _chunker(encoded,code)

    append('EI')
    return code 
Example #3
Source File: pdfutils.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def makeRawImage(filename,IMG=None):
    import zlib
    img = ImageReader(filename)
    if IMG is not None: IMG.append(img)

    imgwidth, imgheight = img.getSize()
    raw = img.getRGBData()

    code = []
    append = code.append
    # this describes what is in the image itself
    append('BI')
    append('/W %s /H %s /BPC 8 /CS /%s /F [/Fl]' % (imgwidth, imgheight,_mode2cs[img.mode]))
    append('ID')
    #use a flate filter
    assert len(raw) == imgwidth * imgheight*_mode2bpp[img.mode], "Wrong amount of data for image"
    compressed = zlib.compress(raw)   #this bit is very fast...

    #append in blocks of 60 characters
    _chunker(compressed,code)

    append('EI')
    return code 
Example #4
Source File: paraparser.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def end_img(self):
        frag = self._stack[-1]
        assert getattr(frag,'_selfClosingTag',''),'Parser failure in <img/>'
        defn = frag.cbDefn = ABag()
        defn.kind = 'img'
        defn.src = getattr(frag,'src',None)
        defn.image = ImageReader(defn.src)
        size = defn.image.getSize()
        defn.width = getattr(frag,'width',size[0])
        defn.height = getattr(frag,'height',size[1])
        defn.valign = getattr(frag,'valign','bottom')
        del frag._selfClosingTag
        self.handle_data('')
        self._pop()

    #### super script 
Example #5
Source File: pdfutils.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def makeA85Image(filename,IMG=None):
    import zlib
    img = ImageReader(filename)
    if IMG is not None: IMG.append(img)

    imgwidth, imgheight = img.getSize()
    raw = img.getRGBData()

    code = []
    append = code.append
    # this describes what is in the image itself
    append('BI')
    append('/W %s /H %s /BPC 8 /CS /%s /F [/A85 /Fl]' % (imgwidth, imgheight,_mode2cs[img.mode]))
    append('ID')
    #use a flate filter and Ascii Base 85
    assert len(raw) == imgwidth * imgheight*_mode2bpp[img.mode], "Wrong amount of data for image"
    compressed = zlib.compress(raw)   #this bit is very fast...
    encoded = _AsciiBase85Encode(compressed) #...sadly this may not be

    #append in blocks of 60 characters
    _chunker(encoded,code)

    append('EI')
    return code 
Example #6
Source File: pdfutils.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def makeRawImage(filename,IMG=None):
    import zlib
    img = ImageReader(filename)
    if IMG is not None: IMG.append(img)

    imgwidth, imgheight = img.getSize()
    raw = img.getRGBData()

    code = []
    append = code.append
    # this describes what is in the image itself
    append('BI')
    append('/W %s /H %s /BPC 8 /CS /%s /F [/Fl]' % (imgwidth, imgheight,_mode2cs[img.mode]))
    append('ID')
    #use a flate filter
    assert len(raw) == imgwidth * imgheight*_mode2bpp[img.mode], "Wrong amount of data for image"
    compressed = zlib.compress(raw)   #this bit is very fast...

    #append in blocks of 60 characters
    _chunker(compressed,code)

    append('EI')
    return code 
Example #7
Source File: pdf.py    From blockdiag with Apache License 2.0 6 votes vote down vote up
def image(self, box, url):
        try:
            image = images.open(url, mode='pillow')
            if image.mode not in ('RGBA', 'L', 'RGB', 'CYMYK'):
                # convert to format that reportlab can recognize
                image = image.convert('RGBA')

            y = self.size[1] - box[3]
            data = ImageReader(image)
            self.canvas.drawImage(data, box.x1, y, box.width, box.height,
                                  mask='auto', preserveAspectRatio=True)
        except IOError:
            pass 
Example #8
Source File: renderPS.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def drawImage(self, image):
        from reportlab.lib.utils import ImageReader
        im = ImageReader(image.path)
        x0 = image.x
        y0 = image.y
        x1 = image.width
        if x1 is not None: x1 += x0
        y1 = image.height
        if y1 is not None: y1 += y0
        self._canvas.drawImage(im._image,x0,y0,x1,y1) 
Example #9
Source File: figures.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, filename, caption, background=None,scaleFactor=None,hAlign='CENTER',border=None):
        assert os.path.isfile(filename), 'image file %s not found' % filename
        from reportlab.lib.utils import ImageReader
        w, h = ImageReader(filename).getSize()
        self.filename = filename
        FlexFigure.__init__(self, w, h, caption, background,scaleFactor=scaleFactor,hAlign=hAlign,border=border) 
Example #10
Source File: img2pdf.py    From TencentComicBook with MIT License 5 votes vote down vote up
def imgs_to_pdf(img_path_list, target_path):
    """将一组图片合成一个pdf文件
    :param str target_path: 输出pdf文件路径
    :param list img_path_list: 要合成的图片的路径列表
    :return str target_path: 输出pdf文件路径
    """
    a4_w, a4_h = portrait(A4)

    c = canvas.Canvas(target_path, pagesize=portrait(A4))
    for img_path in img_path_list:
        img_w, img_h = ImageReader(img_path).getSize()

        if img_w / img_h > a4_w / a4_h:
            # 横图
            ratio = a4_w / img_w
            left_margin = 0
            top_margin = (a4_h - img_h * ratio) / 2
        else:
            # 竖图
            ratio = a4_h / img_h
            left_margin = (a4_w - img_w * ratio) / 2
            top_margin = 0
        c.drawImage(img_path, left_margin, top_margin, img_w * ratio, img_h * ratio)
        c.showPage()
    os.makedirs(os.path.dirname(target_path), exist_ok=True)
    c.save()
    return target_path 
Example #11
Source File: renderPS.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def drawImage(self, image):
        from reportlab.lib.utils import ImageReader
        im = ImageReader(image.path)
        x0 = image.x
        y0 = image.y
        x1 = image.width
        if x1 is not None: x1 += x0
        y1 = image.height
        if y1 is not None: y1 += y0
        self._canvas.drawImage(im._image,x0,y0,x1,y1) 
Example #12
Source File: figures.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, filename, caption, background=None):
        assert os.path.isfile(filename), 'image file %s not found' % filename
        from reportlab.lib.utils import ImageReader
        w, h = ImageReader(filename).getSize()
        self.filename = filename
        FlexFigure.__init__(self, w, h, caption, background) 
Example #13
Source File: pdfjinja.py    From pdfjinja with MIT License 5 votes vote down vote up
def pdf(self):
        stream = BytesIO()
        pdf = canvas.Canvas(stream)
        w, h = self.img.size
        pdf.drawImage(ImageReader(self.img), *self.dimensions)

        if hasattr(self, "label"):
            w, h = self.label.size
            x, y = self.label_x, self.label_y
            pdf.drawImage(ImageReader(self.label), x, y, w, h)

        pdf.save()
        return PdfFileReader(stream).getPage(0) 
Example #14
Source File: pdf.py    From pylinac with MIT License 5 votes vote down vote up
def add_image(self, image_data: io.BytesIO, location: Sequence, dimensions: Sequence, preserve_aspect_ratio: bool=True):
        image_data.seek(0)
        image = ImageReader(Image.open(image_data))
        self.canvas.drawImage(image, location[0]*cm, location[1]*cm, width=dimensions[0]*cm,
                              height=dimensions[1]*cm, preserveAspectRatio=preserve_aspect_ratio) 
Example #15
Source File: pdfgen.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def draw_image(self, image, alpha_channel=None):
        if not image:
            return
        if self.colorspace == uc2const.COLOR_CMYK:
            image = self.cms.convert_image(image, uc2const.IMAGE_CMYK)
        elif self.colorspace == uc2const.COLOR_RGB:
            image = self.cms.convert_image(image, uc2const.IMAGE_RGB)
        elif self.colorspace == uc2const.COLOR_GRAY:
            image = self.cms.convert_image(image, uc2const.IMAGE_GRAY)
        img = ImageReader(image)
        img.getRGBData()
        if alpha_channel:
            img._dataA = ImageReader(alpha_channel)
        self.canvas.drawImage(img, 0, 0, mask='auto') 
Example #16
Source File: pdf.py    From correios with Apache License 2.0 4 votes vote down vote up
def _posting_list_header(self, pdf, width, x1, y1, x2, y2):
        canvas = pdf.canvas

        # logo
        logo = ImageReader(self.posting_list.logo)
        canvas.drawImage(logo, x1, y2 - 10.3 * mm, height=8 * mm, preserveAspectRatio=True, anchor="sw", mask="auto")
        # head1
        canvas.setFont("Helvetica-Bold", size=14)
        canvas.drawCentredString(
            x2 - ((width - 40 * mm) / 2), y2 - 9 * mm, "Empresa Brasileira de Correios e Telégrafos".upper()
        )
        # box
        canvas.setLineWidth(0.5)
        canvas.rect(x1, y2 - 45 * mm, width, 30 * mm)
        canvas.drawCentredString(x1 + width / 2, y2 - (15 * mm) - 15, self.heading_title)
        # header info
        spacer = 5 * mm
        col_width = width / 4
        col = 0
        header = self.header_label_col1.format(
            self.posting_list.number,
            self.posting_list.contract,
            self.posting_list.posting_card.administrative_code,
            self.posting_list.posting_card,
        )
        text = Paragraph(header, style=self.label_style)
        text.wrap(col_width, 30 * mm)
        text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm))
        col = 1
        header = self.header_label_col2.format(
            self.posting_list.sender.name[:30],
            self.posting_list.contract.customer_name[:30],
            self.posting_list.sender.display_address[0][:30],
            self.posting_list.sender.display_address[1][:30],
        )
        text = Paragraph(header, style=self.label_style)
        text.wrap(col_width * 2, 30 * mm)
        text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm))
        col = 3
        header = self.header_label_col3.format(self.posting_list.sender.phone.display())
        text = Paragraph(header, style=self.label_style)
        text.wrap(col_width, 30 * mm)
        text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm))
        code = createBarcodeDrawing(
            "Code128", value=str(self.posting_list.number), width=col_width * 0.6, height=10 * mm, quiet=0
        )
        code.drawOn(canvas, x1 + spacer + col * col_width, y2 - (35 * mm))

    # noinspection PyUnusedLocal