Close Menu
SkytikSkytik

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 2025

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 2025

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 2025
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    SkytikSkytik
    • Home
    • AI Tools
    • Online Tools
    • Tech News
    • Guides
    • Reviews
    • SEO & Marketing
    • Social Media Tools
    SkytikSkytik
    Home»AI Tools»How to Generate QR Codes in Python
    AI Tools

    How to Generate QR Codes in Python

    AwaisBy AwaisDecember 3, 2025No Comments8 Mins Read0 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    How to Generate QR Codes in Python
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    Codes Generate Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Awais
    • Website

    Related Posts

    Escaping the SQL Jungle | Towards Data Science

    March 21, 2026

    A Gentle Introduction to Nonlinear Constrained Optimization with Piecewise Linear Approximations

    March 21, 2026

    Agentic RAG Failure Modes: Retrieval Thrash, Tool Storms, and Context Bloat (and How to Spot Them Early)

    March 21, 2026

    Multi-Hop Data Synthesis for Generalizable Vision-Language Reasoning

    March 21, 2026

    How to Measure AI Value

    March 20, 2026

    What Really Controls Temporal Reasoning in Large Language Models: Tokenisation or Representation of Time?

    March 20, 2026
    Leave A Reply Cancel Reply

    Top Posts

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 20250 Views

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 20250 Views

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 20250 Views
    Don't Miss

    What Is Buttermilk? How It’s Made and Used

    March 22, 2026

    The thickness of buttermilk varies widely from carton to carton. You may need to adjsut…

    Why your law firm’s best leads don’t convert after research

    March 22, 2026

    For Demi Lovato, Learning to Cook Meant Starting to Heal

    March 21, 2026

    Adobe to shut down Marketo Engage SEO tool

    March 21, 2026
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews

    23 Radish Recipes for Salads, Pickles, and More

    March 21, 2026

    Bots could overtake human web usage by 2027

    March 21, 2026
    Most Popular

    13 Trending Songs on TikTok in Nov 2025 (+ How to Use Them)

    November 18, 20257 Views

    How to watch the 2026 GRAMMY Awards online from anywhere

    February 1, 20263 Views

    Corporate Reputation Management Strategies | Sprout Social

    November 19, 20252 Views
    Our Picks

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 2025

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 2025

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 2025

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Facebook X (Twitter) Instagram Pinterest YouTube Dribbble
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    • Disclaimer

    © 2025 skytik.cc. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.