Exemplo n.º 1
0
def datamatrix(data, cellsize=2, with_border=False):
    """Return a datamatrix's image data.

    Powered by the Python library ``qrcode``. See this library's documentation
    for more details.

    Parameters
    ----------
    data
      Data to be encoded in the datamatrix

    cellsize
      size of the picture in inches (?)
    
    with_border
      If false, there will be no border or margin to the datamatrix image.

    Returns
    -------
    image_base64_data
      A string ``data:image/png;base64,xxxxxxxxx`` which you can provide as a
      "src" parameter to a ``<img/>`` tag.

    Examples:
    ---------

    >>> data = datamatrix('EGF')
    >>> html_data = '<img src="%s"/>' % data
    """
    encoder = DataMatrixEncoder(data)
    img_data = encoder.get_imagedata(cellsize=cellsize)
    img = Image.open(BytesIO(img_data))
    if not with_border:
        img = img.crop(ImageOps.invert(img).getbbox())
    return pil_to_html_imgdata(img)
Exemplo n.º 2
0
 def draw_datamatrix(self, text, x, y, size, anchor="C"):
     e_datamatrix = DataMatrixEncoder(text)
     img = ImageReader(io.BytesIO(e_datamatrix.get_imagedata()))
     _x, _y = x - size / 2, y - size / 2
     if "N" in str(anchor).upper(): _y = y - size
     if "S" in str(anchor).upper(): _y = y
     if "E" in str(anchor).upper(): _x = x - size
     if "W" in str(anchor).upper(): _x = x
     self.c.drawImage(img, _x, _y, *[size] * 2)
 def barcode(self,
             barcode_type,
             value,
             width=600,
             height=100,
             humanreadable=0):
     if barcode_type != 'datamatrix':
         return super(ReportAction,
                      self).barcode(barcode_type,
                                    value,
                                    width=width,
                                    height=height,
                                    humanreadable=humanreadable)
     else:
         try:
             barcode = DataMatrixEncoder(value)
             return barcode.get_imagedata(min(75, int(height)))
         except (ValueError, AttributeError):
             raise ValueError("Cannot convert into barcode.")