1

I try to generate a QR Code using QrCode for Python, and i try to use that for lot of objects, so is there any automatic way to groupe lot of images generated in one group, so it will be possible for example, to print it (instead of grouping them manually into an A4 paper).

Abdelouahab
  • 7,331
  • 11
  • 52
  • 82

2 Answers2

1

If you are exporting your PNG files somewhere, you can easily read them again in and print it to a PDF. There is a library called reportlab which is great, as it supports image drawing.

So i am providing here an example code:

I am generating a PNG file, then i am placing it onto the canvas, you can change the size and grouping as you like.

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm

#Create A QR-Code
import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('Some data')
qr.make(fit=True)

img = qr.make_image()
image_file = open("test.png",'w+')
img.save(image_file, "PNG")

#Draw the generated Code on a PDF Canvas
c = canvas.Canvas('ex.pdf')

#Add Single Images
#c.drawImage('test.png', 0, 0, 10*cm, 10*cm)
#c.drawImage('test.png', 10*cm, 10*cm, 10*cm, 10*cm)

#Add Images in a loop
for x in range (0, 3):
for y in range (0, 3):
    c.drawImage('test.png', x*10*cm, y*10*cm, 10*cm, 10*cm)

c.showPage()
c.save()
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • You can run in installing issues on mac with reportlab, http://stackoverflow.com/questions/2252726/how-to-create-pdf-files-in-python this post can solve it. – user1767754 Oct 20 '14 at 21:10
  • now the only problem remaining is deviding the canvas to make the exact amount of images by one paper, thank you, and by the way, here the windows installation http://www.lfd.uci.edu/~gohlke/pythonlibs/#reportlab – Abdelouahab Oct 20 '14 at 21:43
  • 1
    This can be done very easily, when you know the exact size of your qr-code thumbnails. just introduce two for loops one for x and for y – user1767754 Oct 21 '14 at 05:49
1

Create an image of the total size you need, then use paste to place each QR image into the master. The box parameter is used to specify the upper-left coordinate for positioning the QR within the master image.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • how many i can get with it? i guess limited? since i have to use the master as the relative source? or i can make a value that goes 2 times the distance? – Abdelouahab Oct 20 '14 at 21:55
  • @Abdelouahab make the master the size of a sheet of paper (or maybe a little less) and you can place as many as the paper will hold. – Mark Ransom Oct 20 '14 at 22:06
  • sorry, i missunderstood the idea, i thought i use the first image, and then append to her, and not append to a blank picture and fill it with all generated images, thank you again :) – Abdelouahab Oct 20 '14 at 22:11