Close Menu
    Trending
    • America’s coming war over AI regulation
    • “Dr. Google” had its issues. Can ChatGPT Health do better?
    • Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics
    • Why SaaS Product Management Is the Best Domain for Data-Driven Professionals in 2026
    • Stop Writing Messy Boolean Masks: 10 Elegant Ways to Filter Pandas DataFrames
    • What Other Industries Can Learn from Healthcare’s Knowledge Graphs
    • Everyone wants AI sovereignty. No one can truly have it.
    • Yann LeCun’s new venture is a contrarian bet against large language models
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » How to Generate QR Codes in Python
    Artificial Intelligence

    How to Generate QR Codes in Python

    ProfitlyAIBy ProfitlyAIDecember 2, 2025No Comments8 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    to QR Codes

    “QR” in QR code stands for fast response. QR codes are an rising and straightforward strategy to retrieve data with out typing or looking something in your telephone. These codes are basically a sample of black and white squares, with scannable digital codes embedded in them. These days, eating places use these QR Codes to current the menu to their prospects, outlets and marts use them as a type of contactless digital funds, occasion administration groups use them as a fast check-in to their occasions and conferences, and so forth and so forth.

    Producing QR Codes

    As was talked about earlier, QR Codes are developed as a sample or a grid of small black and white squares that retailer data in binary type, that’s, as 0s and 1s. Info is encoded inside the code in particular preparations with the characteristic of customizing the colours, background, and frames, so long as the sample stays the identical.

    The next is an instance of a QR code that has been generated utilizing the Python “qrcode” bundle:

    Hyperlink to my TDS Creator Profile (Picture by Creator)

    Scanning the above code with a scanner that has QR code scanning capabilities will take you to my TDS profile, the place you’ll be able to simply entry beginner-friendly Python initiatives.

    On this article, we’ll undergo the Python Package deal qrcode, putting in it and exploring its totally different capabilities to design QR Codes.

    Putting in the Package deal

    Earlier than beginning the challenge, we’ll set up the related packages. I’m utilizing PyCharm IDE for this activity. As a way to set up the “qrcode” Python bundle, I’ll go to the Python terminal and kind the next:

    pip set up qrcode
    Putting in the qrcode Package deal (Picture by Creator)

    Putting in the QRCode bundle will enable us to create QR codes as PNG information and render them into the Python console. Nonetheless, if we require extra image-related performance, we should always set up the Pillow bundle for picture processing capabilities.

    pip set up "qrcode[pil]"
    Putting in the QRCode bundle with Pillow (Picture by Creator)

    Importing Library and Producing a Easy QR Code

    Now we’ll start the coding. First, we’ll import the library and embrace an alias identify for our ease. As may be seen within the QR Code Python documentation, the QR Code picture for a URL can simply be generated and saved as a PNG utilizing the next traces of code:

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

    This can be a easy QR code that has been created utilizing the make() perform and saved as a PNG file utilizing the save() perform.

    Superior Performance

    For superior picture processing options, we’ll use the QRCode Class within the qrcode Python Package deal.

    Courses and Objects are a helpful idea in programming. Python courses present a blueprint for the creation of objects that share related options, their attributes (variables), and strategies (capabilities). We are going to use the Python Class constructor to create QR codes as objects of that class.

    Beneath is the generic code that enables us to create and save QR codes:

    import qrcode
    qr = qrcode.QRCode(
        model=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(match=True)
    
    img = qr.make_image(fill_color="black", back_color="white")
    img.save("Understanding Courses and Objects.png")
    Understanding Courses and Objects in Python (Picture by Creator)

    The above code has created and saved the QR code as a PNG. When you scan the above code, you’ll land on an attention-grabbing Python Tutorial that explores how the idea of Python courses and objects is carried out in real-world initiatives.

    Now, allow us to deep dive into the generic code above and discover the capabilities and play together with their variations.

    Object Creation

    The primary line of code after importing the qrcode library creates the thing, with its attributes and strategies enclosed inside the spherical brackets.

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

    Right here, qrcode refers back to the Python bundle and QRCode() refers back to the Class.

    Defining the Model

    Subsequent is to outline the model. We will set the integer from 1 to 40, and it will end in totally different sizes of the QR Code.

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

    Let’s create a QR Code with the model variable set to five.

    Model Attribute set to five (Picture by Creator)

    Now allow us to set the model parameter to fifteen:

    Model Attribute set to fifteen (Picture by Creator)

    As may be seen within the two QR Codes above, the model attribute determines the dimensions of the QR Code. The smallest model 1 outputs a QR Code as a 21×21 grid.

    Error Correction

    The subsequent attribute that we’ll undergo is error_correction. The Error Correction attribute offers with the information redundancy, in order that even when the QR Code is broken, it nonetheless stays scannable. There are 4 totally different ranges of error_correction:

    • Error Right L: This gives the bottom degree of error correction, that’s 7%
    • Error Right M: This gives a average degree of knowledge correction, 15% or much less, thus offering a stability between error correction and knowledge measurement
    • Error Right Q: This gives 25% or much less of error correction
    • Error Right H: This gives a excessive degree of error correction and is appropriate for purposes that will have critical harm, and the place knowledge measurement isn’t a priority.
    qr = qrcode.QRCode(
        model=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        ...
        ...
    )

    The extra the proportion worth of error_correction, the extra simply it turns into scannable, even when components of it are broken. On the flip facet, the QR Codes change into bigger in measurement, and this poses an issue when knowledge compression is a principal requirement.

    Allow us to create QR Codes with totally different error_correction varieties.

    Error Correction Sort L (Picture by Creator)
    Error Correction Sort M (Picture by Creator)
    Error Correction Sort Q (Picture by Creator)
    Error Correction Sort H (Picture by Creator)

    As may be seen from the number of the QR Codes generated above, because the error_correction will increase, the complexity of the QR Codes will increase, which means the information measurement will increase, whereas the information redundancy additionally will increase.

    Field Measurement

    The subsequent attribute we’ll discover and play with is the box_size. The box_size within the Python qrcode library refers back to the variety of pixels the QR Code could have.

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

    Allow us to see how our QR Code modifications with totally different values of the box_size:

    Field Measurement = 1 (Picture by Creator)
    Field Measurement = 100 (Picture by Creator)
    Variation of box_size (Picture by Creator)

    The above photos exhibit the distinction when the pixel values modifications, though they is likely to be negligible to the bare eye in relation to small variations.

    Border

    The final attribute that we have to outline to create the thing is the border. This refers back to the surrounding field across the code. The minimal worth is 4, and we will improve it as a lot as we like. Allow us to see how the code modifications with modifications on this attribute:

    Border = 4 (Picture by Creator)
    Border = 10 (Picture by Creator)

    We will see the distinction within the border of the above photos, which might simply be customised with the border variable.

    Including Information

    As soon as our object has been created, with specific values of model, error_correction kind, box_size and border, we’ll now add the information that must be encoded as QR Code. This knowledge generally is a string, a URL, a location, contact data, and so on. This may be simply carried out by means of the add_data() methodology of this class.

    import qrcode
    qr = qrcode.QRCode(
        model=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(match=True)

    The final line of code above calls qr.make(match=True). This routinely matches the QR Code to the information that has been given, after analyzing it. It makes use of the smallest attainable QR Code to accommodate the information, and doesn’t require us to manually set the sizing attributes.

    Producing the Picture Object

    As soon as the QR Code object is created, we’ll use PIL to generate the picture whereas defining its colours, in addition to saving that picture in an acceptable approach. Within the following instance, we’ll set the background to black and fill coloration to pink in our QR Code, as may be seen within the code beneath:

    import qrcode
    qr = qrcode.QRCode(
        model=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(match=True)
    
    img = qr.make_image(fill_color="pink", back_color="black")
    img.save("calc.png")
    Coloration Setting for QR Code (Imae by Creator)

    Conclusion

    On this article, we explored the Python bundle QR Code, which incorporates the blueprint for creating QR codes and saving them in numerous file codecs. It additionally contains superior customisation of the QR code technology, which we have now gone by means of with the understanding of courses and objects. This was a simple and beginner-friendly Python Tutorial that required some surface-level data of courses and objects.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleSå här börjar annonserna smyga sig in i ChatGPT
    Next Article Is There an AI Bubble?
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics

    January 22, 2026
    Artificial Intelligence

    Why SaaS Product Management Is the Best Domain for Data-Driven Professionals in 2026

    January 22, 2026
    Artificial Intelligence

    Stop Writing Messy Boolean Masks: 10 Elegant Ways to Filter Pandas DataFrames

    January 22, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    OpenAI Is Now a For-Profit Company, Paving the Way for a Possible $1 Trillion IPO

    November 4, 2025

    What Counts as AGI? The Test That Could Rewrite One of AI’s Richest Deals

    August 5, 2025

    Cloudflare will now block AI bots from crawling its clients’ websites by default

    July 1, 2025

    Trump’s Executive Order to Eliminate States’ AI Laws

    December 16, 2025

    Moore’s Law • AI Parabellum

    April 3, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Why CrewAI’s Manager-Worker Architecture Fails — and How to Fix It

    November 25, 2025

    Strength in Numbers: Ensembling Models with Bagging and Boosting

    May 15, 2025

    TruthScan vs. Sapling: Which Can Detect AI Writing Better?

    November 24, 2025
    Our Picks

    America’s coming war over AI regulation

    January 23, 2026

    “Dr. Google” had its issues. Can ChatGPT Health do better?

    January 22, 2026

    Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics

    January 22, 2026
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

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