• About
  • Advertise
  • Privacy & Policy
  • Contact
Ai News
Advertisement
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
Ai News
No Result
View All Result
Home Machine Learning

How to Generate QR Codes in Python

AiNEWS2025 by AiNEWS2025
2025-12-02
in Machine Learning
0
How to Generate QR Codes in Python
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


to QR Codes

“QR” in QR code stands for quick response. QR codes are an emerging and easy way to retrieve information without typing or searching anything on your phone. These codes are essentially a pattern of black and white squares, with scannable digital codes embedded in them. Nowadays, restaurants use these QR Codes to present the menu to their customers, shops and marts use them as a form of contactless digital payments, event management teams use them as a quick check-in to their events and conferences, and so forth and so on.

Generating QR Codes

As was mentioned earlier, QR Codes are developed as a pattern or a grid of small black and white squares that store information in binary form, that is, as 0s and 1s. Information is encoded within the code in special arrangements with the feature of customizing the colors, background, and frames, as long as the pattern remains the same.

The following is an example of a QR code that has been generated using the Python “qrcode” package:

Link to my TDS Author Profile (Image by Author)

Scanning the above code with a scanner that has QR code scanning capabilities will take you to my TDS profile, where you can easily access beginner-friendly Python projects.

In this article, we will go through the Python Package qrcode, installing it and exploring its different functions to design QR Codes.

Installing the Package

Before starting the project, we will install the relevant packages. I am using PyCharm IDE for this task. In order to install the “qrcode” Python package, I will go to the Python terminal and type the following:

pip install qrcode
Installing the qrcode Package (Image by Author)

Installing the QRCode package will allow us to create QR codes as PNG files and render them into the Python console. However, if we require more image-related functionality, we should install the Pillow package for image processing capabilities.

pip install "qrcode[pil]"
Installing the QRCode package with Pillow (Image by Author)

Importing Library and Generating a Simple QR Code

Now we will begin the coding. First, we will import the library and include an alias name for our ease. As can be seen in the QR Code Python documentation, the QR Code image for a URL can easily be generated and saved as a PNG using the following lines of code:

import qrcode as qr
img = qr.make("https://pypi.org/project/qrcode/")
img.save("Python QR Code Documentaion.png")
Python QR Code Documentation (Image by Author)

This is a simple QR code that has been created using the make() function and saved as a PNG file using the save() function.

Advanced Functionality

For advanced image processing features, we will use the QRCode Class in the qrcode Python Package.

Classes and Objects are a useful concept in programming. Python classes provide a blueprint for the creation of objects that share similar features, their attributes (variables), and methods (functions). We will use the Python Class constructor to create QR codes as objects of that class.

Below is the generic code that allows us to create and save QR codes:

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('https://towardsdatascience.com/implementing-the-coffee-machine-project-in-python-using-object-oriented-programming/')
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("Understanding Classes and Objects.png")
Understanding Classes and Objects in Python (Image by Author)

The above code has created and saved the QR code as a PNG. If you scan the above code, you will land on an interesting Python Tutorial that explores how the concept of Python classes and objects is implemented in real-world projects.

Now, let us deep dive into the generic code above and explore the functions and play along with their variations.

Object Creation

The first line of code after importing the qrcode library creates the object, with its attributes and methods enclosed within the round brackets.

qr = qrcode.QRCode(
    ...
    ...
    ...
    ...
    ...
)

Here, qrcode refers to the Python package and QRCode() refers to the Class.

Defining the Version

Next is to define the version. We can set the integer from 1 to 40, and this will result in different sizes of the QR Code.

import qrcode
qr = qrcode.QRCode(
    version=1,
    ...
    ...
    ...
    ...
)

Let’s create a QR Code with the version variable set to 5.

Version Attribute set to 5 (Image by Author)

Now let us set the version parameter to 15:

Version Attribute set to 15 (Image by Author)

As can be seen in the two QR Codes above, the version attribute determines the size of the QR Code. The smallest version 1 outputs a QR Code as a 21×21 grid.

Error Correction

The next attribute that we will go through is error_correction. The Error Correction attribute deals with the data redundancy, so that even if the QR Code is damaged, it still remains scannable. There are 4 different levels of error_correction:

  • Error Correct L: This provides the lowest level of error correction, that is 7%
  • Error Correct M: This provides a moderate level of data correction, 15% or less, thus providing a balance between error correction and data size
  • Error Correct Q: This provides 25% or less of error correction
  • Error Correct H: This provides a high level of error correction and is suitable for applications that may have serious damage, and where data size is not a concern.
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    ...
    ...
)

The more the percentage value of error_correction, the more easily it becomes scannable, even when parts of it are damaged. On the flip side, the QR Codes become larger in size, and this poses a problem when data compression is a main requirement.

Let us create QR Codes with different error_correction types.

Error Correction Type L (Image by Author)
Error Correction Type M (Image by Author)
Error Correction Type Q (Image by Author)
Error Correction Type H (Image by Author)

As can be seen from the variety of the QR Codes generated above, as the error_correction increases, the complexity of the QR Codes increases, meaning the data size increases, while the data redundancy also increases.

Box Size

The next attribute we will explore and play with is the box_size. The box_size in the Python qrcode library refers to the number of pixels the QR Code will have.

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    ...
)

Let us see how our QR Code changes with different values of the box_size:

Box Size = 1 (Image by Author)
Box Size = 100 (Image by Author)
Variation of box_size (Image by Author)

The above images demonstrate the difference when the pixel values changes, although they might be negligible to the naked eye when it comes to small differences.

Border

The last attribute that we need to define to create the object is the border. This refers to the surrounding box around the code. The minimum value is 4, and we can increase it as much as we like. Let us see how the code changes with changes in this attribute:

Border = 4 (Image by Author)
Border = 10 (Image by Author)

We can see the difference in the border of the above images, which can easily be customised with the border variable.

Adding Data

Once our object has been created, with particular values of version, error_correction type, box_size and border, we will now add the data that needs to be encoded as QR Code. This data can be a string, a URL, a location, contact information, etc. This can be easily done through the add_data() method of this class.

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=1,
    border=10,
)
qr.add_data('https://towardsdatascience.com/building-a-facial-recognition-model-using-pca-svm-algorithms-c81d870add16/')
qr.make(fit=True)

The last line of code above calls qr.make(fit=True). This automatically fits the QR Code to the data that has been given, after analyzing it. It uses the smallest possible QR Code to accommodate the data, and does not require us to manually set the sizing attributes.

Generating the Image Object

Once the QR Code object is created, we will use PIL to generate the image while defining its colors, as well as saving that image in a suitable way. In the following example, we will set the background to black and fill color to pink in our QR Code, as can be seen in the code below:

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=1,
    border=10,
)
qr.add_data('https://towardsdatascience.com/using-python-to-build-a-calculator/')
qr.make(fit=True)

img = qr.make_image(fill_color="pink", back_color="black")
img.save("calc.png")
Color Setting for QR Code (Imae by Author)

Conclusion

In this article, we explored the Python package QR Code, which includes the blueprint for creating QR codes and saving them in different file formats. It also includes advanced customisation of the QR code generation, which we have gone through with the understanding of classes and objects. This was an easy and beginner-friendly Python Tutorial that required some surface-level knowledge of classes and objects.

Source link

#Generate #Codes #Python

Tags: codingEditors PickProgrammingPythonQr Code
Previous Post

The missile meant to strike fear in Russia’s enemies fails once again

Next Post

An AI model trained on prison phone calls now looks for planned crimes in those calls

AiNEWS2025

AiNEWS2025

Next Post
An AI model trained on prison phone calls now looks for planned crimes in those calls

An AI model trained on prison phone calls now looks for planned crimes in those calls

Stay Connected test

  • 23.9k Followers
  • 99 Subscribers
  • Trending
  • Comments
  • Latest
A tiny new open source AI model performs as well as powerful big ones

A tiny new open source AI model performs as well as powerful big ones

0
Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

0
Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

0
Best Headphones for Working Out (2024): Bose, Shokz, JLab

Best Headphones for Working Out (2024): Bose, Shokz, JLab

0
Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

2025-12-23
The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

2025-12-23
In a surprise announcement, Tory Bruno is out as CEO of United Launch Alliance

In a surprise announcement, Tory Bruno is out as CEO of United Launch Alliance

2025-12-23
The FCC’s foreign drone ban is here

The FCC’s foreign drone ban is here

2025-12-23

Recent News

Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

2025-12-23
The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

2025-12-23
In a surprise announcement, Tory Bruno is out as CEO of United Launch Alliance

In a surprise announcement, Tory Bruno is out as CEO of United Launch Alliance

2025-12-23
The FCC’s foreign drone ban is here

The FCC’s foreign drone ban is here

2025-12-23
Footer logo

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow Us

Browse by Category

  • AI & Cloud Computing
  • AI & Cybersecurity
  • AI & Sentiment Analysis
  • AI Applications
  • AI Ethics
  • AI Future Predictions
  • AI in Education
  • AI in Fintech
  • AI in Gaming
  • AI in Healthcare
  • AI in Startups
  • AI Innovations
  • AI News
  • AI Research
  • AI Tools & Automation
  • Apps
  • AR/VR & AI
  • Business
  • Deep Learning
  • Emerging Technologies
  • Entertainment
  • Fashion
  • Food
  • Gadget
  • Gaming
  • Health
  • Lifestyle
  • Machine Learning
  • Mobile
  • Movie
  • Music
  • News
  • Politics
  • Review
  • Robotics & Smart Systems
  • Science
  • Sports
  • Startup
  • Tech
  • Travel
  • World

Recent News

Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

Scaling Auditable Agentic Workflows in Financial Services – with Leaders from Moody’s and Prudential Insurance

2025-12-23
The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

The Machine Learning “Advent Calendar” Day 22: Embeddings in Excel

2025-12-23
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.