-1.2 C
United States of America
Thursday, December 5, 2024

Grasp QR Code Creation and Customization with Python


QR codes might be seen in all places . Particularly on eating places , petrol pumps , outlets primarily for making funds. There are a number of different functions additionally. The principle motive is it’s a straightforward option to retailer info within the type of a small, easy-to-scan picture . On this information, I’ll make it easier to mastering QR codes and the way QR codes work and stroll you thru creating them utilizing python.

Studying Outcomes

  • Perceive the basics of QR codes and their performance.
  • Mastering QR Codes lets you effectively create, customise, and apply QR codes in varied real-world situations utilizing Python.
  • Learn to generate and customise QR codes utilizing Python.
  • Discover the historical past and benefits of QR codes in varied functions.
  • Acquire hands-on expertise by making a Wi-Fi QR code for straightforward community entry.
  • Uncover real-world makes use of for QR codes, together with in public areas and companies.

This text was revealed as part of the Information Science Blogathon.

What are QR Codes?

Fast Response(QR) codes are literally two-dimensional matrix barcode that may retailer varied varieties of information, comparable to URLs, textual content, contact info, or Wi-Fi credentials .Fast Response indicating that The code contents ought to be decoded in a short time at excessive pace .Normally the code consists of black modules organized in a sq. sample on a white background. Not like conventional barcodes, which solely maintain info in a single course (horizontally). QR codes can retailer information each horizontally and vertically. Permitting for a lot better storage capability.

Historical past of QR Codes

It was invented by Denso Wave in 1994 for Toyota Group .The aim was to trace automotive components throughout manufacturing. Their skill was to retailer a major quantity of information and might be scanned shortly made them well-liked in varied industries over time.

Why Use QR Codes?

QR codes have a number of benefits:

  • Simple to Use : It may be scanned with a smartphone digicam or a devoted QR scanner.
  • Versatile : can retailer several types of information, together with URLs, textual content, and extra.
  • Quick Entry : shortly offers info with only a scan.
  • Contactless : helpful for contactless transactions and sharing.

QR Codes with Python

On this part, I’ll clarify how you can generate QR codes utilizing python. We are going to cowl completely different examples to show the method.Ranging from making a easy QR code to customizing it additional. We’ll use the qrcode library, which offers an easy option to create QR codes. To start, be sure to have the library put in:

pip set up qrcode[pil]

Example1 : Producing a Easy QR Code

On this first instance, create a fundamental QR code with default settings. Right here’s the code:

import qrcode
from PIL import Picture

#Information to be encoded
information = "Welcome to QR Code Tutorial"

#Create a QR code object
qr = qrcode.QRCode(
    model=1,    # Measurement of the QR code
    box_size=10,  # Measurement of every field within the QR code grid
    border=4      # Border thickness
)
qr.add_data(information)
qr.make(match=True)

#Generate QR code picture
img = qr.make_image(fill="black", back_color="white")
img.present()
img.save('simple_qr_code.png')

Right here’s a clarification for the parameters used within the code:

  • model: It would controls the code’s complexity and dimension.
  • box_size: dimension of every field within the QR code.
  • border: the thickness of the outer white house.

The above code will generate a easy QR code encoding the textual content “Welcome to QR Code Tutorial.” The next would be the end result ,merely scan the code to view the output.

QR code1

Example2: Producing a QR Code with Customized Colours

Right here  what we’re doing is make the  QR code extra visually interesting.Inorder to realize this we’ve got to both change foreground shade or background shade or change each.On this instance i hvae modified each foreground and background colours.You may tryout the code with completely different shade mixtures.

import qrcode
from PIL import Picture

#Information to be encoded
information = "Welcome to QR Code Tutorial"

#Create a QR code object
qr = qrcode.QRCode(
    model=1,    # Measurement of the QR code
    box_size=10,  # Measurement of every field within the QR code grid
    border=4      # Border thickness
)
qr.add_data(information)
qr.make(match=True)

#Create a QR code with customized colours
img_colored = qr.make_image(fill_color="darkgreen", back_color="lightyellow")
img_colored.present()
img_colored.save('custom_color_qr_code.png')

The above code will generate a QR  code with a darkish inexperienced foreground and lightweight yellow background. Scan the code to view the output with these customized colours.

Mastering QR code

Example3: QR Code Era from Analytics Vidhya URL

On this instance, making a QR code that encodes the URL of Analytics Vidhya. This enables anybody who scans the code to straight go to the web site. Right here’s the code:

import qrcode
from PIL import Picture

#Create the QR code as typical
qr = qrcode.QRCode(
    model=5,    # Model adjusted to incorporate the brand
    box_size=10,
    border=4
)
qr.add_data("https://www.analyticsvidhya.com/")
qr.make(match=True)

#Generate the QR code picture
img = qr.make_image(fill="black", back_color="white")

#Save and present the end result
img.save('QR_code_AnalyticsVidhya.png')
img.present()

Simply scan it. The QR will direct you in direction of official web page of Analytics Vidhya

QR code Generation

Example4: QR Code with Embedded Brand and URL Mixed

Right here we’ll tryout a customized QR code that not solely hyperlinks to the Analytics Vidhya web site but additionally contains a customized brand embedded proper within the middle.It can provide  a particular and branded look. Let’s dive into the code instance 

import qrcode
from PIL import Picture

# Create the QR code as typical
qr = qrcode.QRCode(
    model=5,  # Model adjusted to incorporate a brand
    box_size=10,
    border=4
)
qr.add_data("https://www.analyticsvidhya.com/")
qr.make(match=True)

# Generate the QR code picture
img = qr.make_image(fill="black", back_color="white")

# Open the brand picture
brand = Picture.open('AV_logo.png')

# Calculate the scale for the brand
logo_size = 100  # Set this in keeping with your wants
brand = brand.resize((logo_size, logo_size), Picture.Resampling.LANCZOS)  # Use LANCZOS for high-quality downsampling

# Paste the brand onto the QR code
pos = ((img.dimension[0] - logo_size) // 2, (img.dimension[1] - logo_size) // 2)
img.paste(brand, pos, masks=brand)

# Save and present the end result
img.save('QR_code_with_AnalyticsVidhya_Logo.png')
img.present()

The next cropped picture is an instance that you could attempt along with your code:

AV Logo

This model of code provides a contact of pleasure and emphasizes the distinctive, branded side of the QR code.

Mastering QR Code: AV

Example5: Studying QR Codes from an Picture

Right here, I’ll present how you can learn and decode a QR code from a picture utilizing OpenCV. That is helpful when you’ll want to extract information from a QR code that’s already been created or shared in a picture file. Right here’s the code:Simply take the earlier output QR Code picture as new enter,then check out the next code

# !pip set up opencv-python
import cv2

# Learn the picture file
picture = cv2.imread('/residence/Tutorials/Analytics Vidya/QR_code_with_AnalyticsVidhya_Logo.png')

# Initialize the QR code detector
detector = cv2.QRCodeDetector()

# Detect and decode the QR code
information, vertices_array, _ = detector.detectAndDecode(picture)

if vertices_array isn't None:
    print(f"Decoded Information: {information}")
else:
    print("QR code not detected.")

The output shall be:

"   Decoded Information: https://www.analyticsvidhya.com/   "

Mini-Mission : Making a Wi-Fi QR Code

QR code is a straightforward option to enable customers to attach with a community. By simply scanning the code. Utilizing this we will keep away from manually typing wifi title or password . We will implement a qr code that accommodates all the mandatory info.

On this mini venture ,I’ll information you  to construct a QR code to retailer wifi particulars.It may be a lot helpful in public locations, places of work in addition to for private use.The generated qr code will retailer wifi credentials, such because the community title (SSID), password, and safety kind (in my case WPA2) ,permitting gadgets to attach with ease.

Inorder to tryout this mini venture,1st you must findout your Wi-Fi SSID,Wi-Fi Safety Kind and Password utilizing following linux instructions within the terminal.Bear in mind , attempt one after one other

#1 Discover the Wi-Fi SSID (Community Identify)
nmcli -t -f energetic,ssid dev wifi | grep '^sure'

#2 Get Wi-Fi Safety Kind
# right here my SSID Identify is RAGAI_4G

nmcli -f ssid,safety dev wifi | grep "RAGAI_4G" 

#3Get Password
sudo grep -r "psk=" /and so forth/NetworkManager/system-connections/

Code Implementation

Allow us to now implement the code under:

# Import the mandatory library
import qrcode

# Outline Wi-Fi credentials
wifi_ssid = "RAGAI_4G"#substitute along with your SSID Identify
wifi_password = "PASSWORD" #Ur PASSWORD
wifi_security = "WPA2"  # Might be 'WEP', 'WPA', or 'nopass' (no password)

# Create the QR code information within the format that encodes Wi-Fi info
wifi_data = f"WIFI:T:{wifi_security};S:{wifi_ssid};P:{wifi_password};;"

# Create a QR code object with desired settings
qr = qrcode.QRCode(
    model=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4
)

# Add the Wi-Fi information to the QR code object
qr.add_data(wifi_data)
qr.make(match=True)

# Generate the QR code picture
img = qr.make_image(fill="black", back_color="white")

# Save the QR code picture to a file
img.save('wifi_qr_code.png')

# Show the QR code picture (for testing)
img.present()

print("Wi-Fi QR code saved efficiently")

Scanning the QR Code

Anticipating you’ve got efficiently tried out the code i’ve shared.Now  you’ve got the output QR code picture.Simply can scan it with a smartphone or scanner to connect with the wifi community. Observe the steps one after the other:

  • Open the digicam app or QR scanner in your cellphone.
  • Level the digicam on the QR code.
  • Android telephones will show the Wi-Fi community title and immediate to attach mechanically.

Bear in mind ,in case your cellphone doesnot have inbuilt functions,you may obtain any scanner functions from playstore.

Sensible Use Circumstances for QR Codes

This Wi-Fi QR code might be particularly useful in:

  • Public Areas: In cafes, libraries,pumps or accommodations the place guests can merely scan the code to attach while not having to ask for the credentials.
  • Dwelling Networks: Giving family and friends a straightforward approach to connect with your community with out sharing your password.
  • Enterprise Settings: Sharing community entry with visitors or workers with out compromising safety.

Conclusion

The examples on this article  may also help you  for varied functions and each in your passion tasks and actual time tasks.If any capabilities happen in your house you may invite your folks by saring the situation info with the assistance of a qr code.In case you are at workplace or faculty the identical might be utilized for occasion scheduling and kind filling.

Right here is the Github hyperlink.

Key Takeaways

  • QR codes are versatile instruments that retailer varied information sorts, from URLs to Wi-Fi credentials.
  • Python’s qrcode library simplifies the method of making and customizing QR codes.
  • QR codes improve consumer expertise by enabling quick, contactless info sharing.
  • Customizing QR codes with colours or logos can enhance branding and visible enchantment.
  • Wi-Fi QR codes present a seamless option to join gadgets to a community with out guide entry.
  • Mastering QR Codes unlocks the potential to streamline information sharing, improve consumer experiences, and enhance entry to info shortly and successfully.

Often Requested Questions

Q1. What’s a QR code?

A. A QR code is a two-dimensional barcode that shops info comparable to URLs, textual content, or Wi-Fi credentials, and might be scanned shortly by a digicam or scanner.

Q2. How do I generate a QR code utilizing Python?

A. You need to use the qrcode library in Python to generate QR codes by putting in it with pip set up qrcode[pil] and making a QR code object along with your information.

Q3. Can I customise the looks of a QR code?

A. Sure, you may change the foreground and background colours, and even embed logos throughout the QR code for a extra personalised look.

This autumn. What’s a Wi-Fi QR code?

A. A Wi-Fi QR code shops community credentials (SSID, password, and safety kind) so customers can connect with a community by merely scanning the code.

Q5. Can QR codes be used for functions apart from fee?

A. Sure, QR codes serve varied functions, comparable to sharing URLs, contact info, occasion particulars, and even Wi-Fi credentials.

Q6. What’s the significance of mastering QR codes?

A. Mastering QR codes lets you simply create and customise codes for varied functions, comparable to sharing URLs, Wi-Fi credentials, and extra, enhancing comfort and accessibility in digital interactions.

The media proven on this article isn’t owned by Analytics Vidhya and is used on the Creator’s discretion.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles