[reportlab-users] QR Codes

Robin Becker robin at reportlab.com
Wed Sep 15 09:32:06 EDT 2010


On 15/09/2010 14:05, Germán M. Bravo wrote:

> Hey! how do you get that as an image to see it on the web? (as in the demo)

>

> Also, I was wondering about those white borders in-between the squares (same happens with regular barcodes) ...when we had the strokes on, those were covered when you zoomed in, but strokes messed up the printed version... I'm not sure, you think there could be an issue somewhere in the primitives for the PDF canvas? if you try with other primitives for other devices, does it have the same effect of leaving those tiny white borders? I saw ReportLab can print to other things other than PDF, but I haven't gotten into that yet... and the math in the barcodes (QR as well) seems okay, so I was wondering if there's a bug somewhere or something.

>

> Cheers,

> Germán

........

The code for producing the image is exactly this


> def qrImage(request):

> if request.method!='GET': return handle405(['GET'])

> from reportlab.graphics.shapes import Drawing

> from reportlab.graphics.barcode.qr import QrCodeWidget

>

> text = 'ReportLab Demo Output: '+request.GET['text']

> qrw = QrCodeWidget(text)

>

> b = qrw.getBounds()

>

> w=b[2]-b[0]

> h=b[3]-b[1]

> d = Drawing(360,360,transform=[360./w,0,0,360./h,0,0])

>

> d.add(qrw)

> #d.save(formats=['pdf','gif'],outDir='.',fnRoot='QR')

> imgfmt = 'gif'

> return HttpResponse(d.asString(imgfmt), 'image/%s' % imgfmt)



so we get the widget and add it to a Drawing with a transformation matrix to
adjust the height/width and then render the drawing as a gif to a string.

I see the white bordery lines, but am entirely unsure how they get there. I
suspect that the rectangles aren't exactly pixel aligned in the gif so then our
back end tries to approximate half a pixel being drawn or something. I suppose
if we wanted to draw it exactly we'd need to draw at the correct scale, take
account of the pixellated nature of the output and be sure not to have any
anti-aliasing etc etc. I don't think that's at all easy in our case as we don't
have an antialising backend (except maybe direct to PIL). Certainly you'd have
to fix the feature size to be a whole number of pixels and then draw from there.
--
Robin Becker


More information about the reportlab-users mailing list