Reading and Writing QR Codes

Learn how to generate and read different kinds of QR codes.

A quick way to generate a QR code

Here is a quick example that shows how to generate a QR code containing a link to the Educative website:

Press + to interact
import qrcode
data = 'https://educative.io'
img = qrcode.make(data)
print(type(img))
img.save('output/qrcode.png')

Line 1: First, we import the qrcode library.

Line 2: Then, we define the data we wish to encode in the QR code. In this case, we use the URL of the Educative website. This data can be any kind of character string data. It doesn’t have to be a URL.

Line 3: By using the qrcode.make() function, we generate a QR code using the data we just defined. The output is a PIL image. Consequently, we can apply all the image operations from the PIL library to the resulting image.

Line 4: We show that the type of output from the above function is indeed an image. For this, we use a print(type()) statement.

Line 5: Finally, we use the save() method on the PIL image to save the QR code image to a PNG file.

More options for generating a QR code

The qrcode library also allows us to create QR codes ...