Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Lessons Learned from Upgrading to LangChain 1.0 in Production
    • What even is the AI bubble?
    • Dog breeds carry wolf DNA, new study finds genetic advantages
    • London-based PolyAI raises €73.2 million to scale its enterprise conversational AI platform
    • The Best Meteor Shower of the Year Is Coming—Here’s How to Watch
    • How Nvidia’s lobbying efforts grew after Howard Lutnick brokered Jensen Huang’s access to Trump, ending with the president’s approval of the H200 sales to China (Financial Times)
    • Today’s NYT Mini Crossword Answers for Dec. 15
    • Roomba vacuum cleaner firm iRobot files for bankruptcy
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Monday, December 15
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»How to Generate QR Codes in Python
    Artificial Intelligence

    How to Generate QR Codes in Python

    Editor Times FeaturedBy Editor Times FeaturedDecember 3, 2025No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    to QR Codes

    “QR” in QR code stands for fast response. QR codes are an rising and simple technique to retrieve data with out typing or looking something in your cellphone. These codes are primarily 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 clients, 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 kind, that’s, as 0s and 1s. Data is encoded inside the code in particular preparations with the function 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” package deal:

    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 tasks.

    On this article, we are going to undergo the Python Bundle qrcode, putting in it and exploring its completely different features to design QR Codes.

    Putting in the Bundle

    Earlier than beginning the undertaking, we are going to set up the related packages. I’m utilizing PyCharm IDE for this activity. With a purpose to set up the “qrcode” Python package deal, I’ll go to the Python terminal and kind the next:

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

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

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

    Importing Library and Producing a Easy QR Code

    Now we are going to start the coding. First, we are going to import the library and embrace an alias identify for our ease. As could 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/undertaking/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 are going to use the QRCode Class within the qrcode Python Bundle.

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

    Under is the generic code that permits 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. In case you scan the above code, you’ll land on an attention-grabbing Python Tutorial that explores how the idea of Python lessons and objects is applied in real-world tasks.

    Now, allow us to deep dive into the generic code above and discover the features 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 package deal and QRCode() refers back to the Class.

    Defining the Model

    Subsequent is to outline the model. We are able to set the integer from 1 to 40, and this can lead to completely 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 could 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 are going to 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 completely different ranges of error_correction:

    • Error Appropriate L: This offers the bottom stage of error correction, that’s 7%
    • Error Appropriate M: This offers a reasonable stage of information correction, 15% or much less, thus offering a steadiness between error correction and information dimension
    • Error Appropriate Q: This offers 25% or much less of error correction
    • Error Appropriate H: This offers a excessive stage of error correction and is appropriate for purposes which will have severe harm, and the place information dimension isn’t a priority.
    qr = qrcode.QRCode(
        model=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        ...
        ...
    )

    The extra the share worth of error_correction, the extra simply it turns into scannable, even when components of it are broken. On the flip aspect, the QR Codes turn out to be bigger in dimension, and this poses an issue when information compression is a fundamental requirement.

    Allow us to create QR Codes with completely 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 could 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 dimension will increase, whereas the information redundancy additionally will increase.

    Field Measurement

    The subsequent attribute we are going to 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 may have.

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

    Allow us to see how our QR Code adjustments with completely 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 reveal the distinction when the pixel values adjustments, though they could be negligible to the bare eye relating 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 are able to improve it as a lot as we like. Allow us to see how the code adjustments with adjustments on this attribute:

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

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

    Including Knowledge

    As soon as our object has been created, with specific values of model, error_correction sort, box_size and border, we are going to now add the information that must be encoded as QR Code. This information generally is a string, a URL, a location, contact data, and so forth. This may be simply performed via the add_data() technique 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 robotically matches the QR Code to the information that has been given, after analyzing it. It makes use of the smallest potential 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 are going to 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 are going to set the background to black and fill coloration to pink in our QR Code, as could 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")
    Shade Setting for QR Code (Imae by Creator)

    Conclusion

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



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Lessons Learned from Upgrading to LangChain 1.0 in Production

    December 15, 2025

    The Machine Learning “Advent Calendar” Day 14: Softmax Regression in Excel

    December 14, 2025

    The Skills That Bridge Technical Work and Business Impact

    December 14, 2025

    Stop Writing Spaghetti if-else Chains: Parsing JSON with Python’s match-case

    December 14, 2025

    How to Increase Coding Iteration Speed

    December 13, 2025

    The Machine Learning “Advent Calendar” Day 13: LASSO and Ridge Regression in Excel

    December 13, 2025
    Leave A Reply Cancel Reply

    Editors Picks

    Lessons Learned from Upgrading to LangChain 1.0 in Production

    December 15, 2025

    What even is the AI bubble?

    December 15, 2025

    Dog breeds carry wolf DNA, new study finds genetic advantages

    December 15, 2025

    London-based PolyAI raises €73.2 million to scale its enterprise conversational AI platform

    December 15, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    Royal Enfield hits record 1-million mark in annual motorcycle sales

    April 19, 2025

    Gifts for Gym Bros: Best Workout Gear and Gym Essentials (2025)

    November 17, 2025

    Eating cheese before bed may cause nightmares, study finds

    July 1, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

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