Create a QR Code with 7 Lines of Python

Jacob Naryan - Full-Stack Developer

Posted: Sat Aug 05 2023

Last updated: Wed Apr 03 2024

Have you ever needed to make a QR code, but found you either need to pay for some service you’re never going to use again, or needed to make an account and sign up for spam emails? Will look no further, I have found the solution for you. I’ve made a simple Python program that anyone who knows how to use a computer can use.

1. Install the qrcode library

The qrcode library is a python library for generating QR codes. To install it, run the following command in your terminal:

pip install qrcode

2. Import the qrcode library

Once installed, you can import the qrcode library in your python project by adding the following code:

import qrcode

3. Create a QR Code Object

Next, you need to create a QR Code object. You can do this by initializing a QRCode instance with the version, box size, and border you want for the QR code. For example:

qr = qrcode.QRCode(version=1, box_size=10, border=5)

4. Set the URL

Next, you need to collect the URL you are going to be using for the QR code and set it. To do so we add:

data = input("Enter the url you want as a QR code: ")
qr.add_data(data)

5. Generate the QR Code

Now that you’ve created the QR Code object, you need to generate the actual QR code image. To do this, you need to add the following code:

qr.make(fit=True)

This will generate the QR code image based on the data contained in the QR Code object you created in Step 3.

6. Add an Image File Extension

Now that you have generated the QR code image, you need to add an image file extension to it. You can do this by adding the following code:

img = qr.make_image(fill_color="black", back_color="white")

This will create an image object with a black foreground and white background, which is suitable for printing or displaying on a screen.

7. Save the Image

To save the image to your computer, you need to add the following code:

img.save("qrcode.png")

This will save the image file as “qrcode.png” in your current working directory. That’s it! You’ve successfully created your QR code.

If you liked this blog, check out my personal blog for more content like this.

Check out the video I made on the same here:

Thanks for reading, please consider tipping some BTC if you liked the blog!
Click or scan the QR code below.

bitcoin address for tips if you are so inclined.