0
0
PythonProgramBeginner · 2 min read

Python Program to Generate QR Code Easily

Use the qrcode library in Python to generate a QR code by calling qrcode.make('your text') and saving it as an image with .save('filename.png').
📋

Examples

InputHello, world!
OutputA QR code image file named 'qrcode.png' containing 'Hello, world!'
Inputhttps://www.example.com
OutputA QR code image file named 'qrcode.png' containing the URL 'https://www.example.com'
Input
OutputA QR code image file named 'qrcode.png' containing an empty string (blank QR code)
🧠

How to Think About It

To generate a QR code, first decide what text or URL you want to encode. Then use a QR code library to convert that text into a QR code image. Finally, save the image to a file so you can open or share it.
📐

Algorithm

1
Import the QR code library.
2
Create a QR code object with the desired text or URL.
3
Generate the QR code image.
4
Save the image to a file.
💻

Code

python
import qrcode

# Data to encode
data = "https://www.example.com"

# Generate QR code
img = qrcode.make(data)

# Save as PNG file
img.save("qrcode.png")

print("QR code generated and saved as qrcode.png")
Output
QR code generated and saved as qrcode.png
🔍

Dry Run

Let's trace generating a QR code for 'https://www.example.com' through the code

1

Import qrcode library

The program loads the qrcode module to access QR code functions.

2

Set data to encode

The variable data is set to 'https://www.example.com'.

3

Generate QR code image

qrcode.make(data) creates an image object representing the QR code.

4

Save image to file

The image is saved as 'qrcode.png' on disk.

5

Print confirmation

The program prints 'QR code generated and saved as qrcode.png'.

StepActionValue
1Import qrcodeqrcode module loaded
2Set data'https://www.example.com'
3Generate QR codeImage object created
4Save image'qrcode.png' file created
5Print messageQR code generated and saved as qrcode.png
💡

Why This Works

Step 1: Importing the library

The qrcode library provides easy functions to create QR codes from text.

Step 2: Creating the QR code

Calling qrcode.make(data) converts the text into a QR code image object.

Step 3: Saving the image

The image object has a .save() method to write the QR code as a PNG file.

🔄

Alternative Approaches

Using qrcode.QRCode for more control
python
import qrcode

qr = qrcode.QRCode(version=1, box_size=10, border=4)
qr.add_data('Hello QR')
qr.make(fit=True)
img = qr.make_image(fill_color='black', back_color='white')
img.save('custom_qrcode.png')
print('Custom QR code saved as custom_qrcode.png')
This method allows customizing size and colors but requires more code.
Using segno library
python
import segno

qr = segno.make('https://www.example.com')
qr.save('segno_qrcode.png')
print('QR code generated with segno and saved as segno_qrcode.png')
Segno is another QR code library that is lightweight and fast.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the length of the input text n, as encoding longer data takes more processing.

Space Complexity

The space used is proportional to the size of the QR code image generated, which depends on the data length.

Which Approach is Fastest?

Using qrcode.make() is simple and fast for most uses; the QRCode class offers more control but is slightly slower.

ApproachTimeSpaceBest For
qrcode.make()O(n)O(n)Quick and simple QR code generation
qrcode.QRCode classO(n)O(n)Customizable QR codes with size and color options
segno libraryO(n)O(n)Lightweight alternative with fast generation
💡
Install the qrcode library first using pip install qrcode[pil] to handle images.
⚠️
Forgetting to install the required library or not saving the QR code image to a file.