14 C
New York
Tuesday, November 19, 2024

Grasp QR Code Creation and Customization with Python


QR codes could be seen all over the place . Particularly on eating places , petrol pumps , retailers primarily for making funds. There are a number of different functions additionally. The principle motive is it’s a simple method to retailer data within the type of a small, easy-to-scan picture . On this information, I’ll enable you 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.
  • Discover ways to generate and customise QR codes utilizing Python.
  • Discover the historical past and benefits of QR codes in varied functions.
  • Achieve hands-on expertise by making a Wi-Fi QR code for simple 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 Knowledge Science Blogathon.

What are QR Codes?

Fast Response(QR) codes are literally two-dimensional matrix barcode that may retailer varied sorts of knowledge, equivalent to URLs, textual content, contact data, 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 data in a single course (horizontally). QR codes can retailer knowledge each horizontally and vertically. Permitting for a lot larger 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 big quantity of knowledge and could be scanned rapidly made them widespread 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 digital camera or a devoted QR scanner.
  • Versatile : can retailer several types of knowledge, together with URLs, textual content, and extra.
  • Quick Entry : rapidly supplies data with only a scan.
  • Contactless : helpful for contactless transactions and sharing.

QR Codes with Python

On this part, I’ll clarify the right way to generate QR codes utilizing python. We are going to cowl completely different examples to exhibit the method.Ranging from making a easy QR code to customizing it additional. We’ll use the qrcode library, which supplies an easy method to create QR codes. To start, ensure you have the library put in:

pip set up qrcode[pil]

Example1 : Producing a Easy QR Code

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

import qrcode
from PIL import Picture

#Knowledge to be encoded
knowledge = "Welcome to QR Code Tutorial"

#Create a QR code object
qr = qrcode.QRCode(
    model=1,    # Dimension of the QR code
    box_size=10,  # Dimension of every field within the QR code grid
    border=4      # Border thickness
)
qr.add_data(knowledge)
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 is going to 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 to both change foreground shade or background shade or change each.On this instance i hvae modified each foreground and background colours.You possibly can tryout the code with completely different shade combos.

import qrcode
from PIL import Picture

#Knowledge to be encoded
knowledge = "Welcome to QR Code Tutorial"

#Create a QR code object
qr = qrcode.QRCode(
    model=1,    # Dimension of the QR code
    box_size=10,  # Dimension of every field within the QR code grid
    border=4      # Border thickness
)
qr.add_data(knowledge)
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 permits 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 normal
qr = qrcode.QRCode(
    model=5,    # Model adjusted to incorporate the emblem
    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 the direction of official web page of Analytics Vidhya

QR code Generation

Example4: QR Code with Embedded Brand and URL Mixed

Right here we are going to tryout a customized QR code that not solely hyperlinks to the Analytics Vidhya web site but additionally encompasses a customized emblem embedded proper within the heart.It may give  a particular and branded look. Let’s dive into the code instance 

import qrcode
from PIL import Picture

# Create the QR code as normal
qr = qrcode.QRCode(
    model=5,  # Model adjusted to incorporate a emblem
    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 emblem picture
emblem = Picture.open('AV_logo.png')

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

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

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

The next cropped picture is an instance you could strive together with your code:

AV Logo

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

Mastering QR Code: AV

Example5: Studying QR Codes from an Picture

Right here, I’ll present the right way to learn and decode a QR code from a picture utilizing OpenCV. That is helpful when it’s essential to extract knowledge 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
knowledge, vertices_array, _ = detector.detectAndDecode(picture)

if vertices_array is just not None:
    print(f"Decoded Knowledge: {knowledge}")
else:
    print("QR code not detected.")

The output will likely be:

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

Mini-Mission : Making a Wi-Fi QR Code

QR code is a straightforward method to permit customers to attach with a community. By simply scanning the code. Utilizing this we are able to keep away from manually typing wifi identify or password . We will implement a qr code that accommodates all the required data.

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

Inorder to tryout this mini mission,1st it’s a must to findout your Wi-Fi SSID,Wi-Fi Safety Kind and Password utilizing following linux instructions within the terminal.Keep in mind , strive one after one other

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

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

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

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

Code Implementation

Allow us to now implement the code beneath:

# Import the required library
import qrcode

# Outline Wi-Fi credentials
wifi_ssid = "RAGAI_4G"#exchange together with your SSID Title
wifi_password = "PASSWORD" #Ur PASSWORD
wifi_security = "WPA2"  # May be 'WEP', 'WPA', or 'nopass' (no password)

# Create the QR code knowledge within the format that encodes Wi-Fi data
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 knowledge 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 might have efficiently tried out the code i’ve shared.Now  you might have 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 digital camera app or QR scanner in your telephone.
  • Level the digital camera on the QR code.
  • Android telephones will show the Wi-Fi community identify and immediate to attach robotically.

Keep in mind ,in case your telephone doesnot have inbuilt functions,you’ll be able to obtain any scanner functions from playstore.

Sensible Use Instances for QR Codes

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

  • Public Areas: In cafes, libraries,pumps or inns the place guests can merely scan the code to attach while not having to ask for the credentials.
  • Residence Networks: Giving family and friends a simple method to connect with your community with out sharing your password.
  • Enterprise Settings: Sharing community entry with company or staff with out compromising safety.

Conclusion

The examples on this article  will help you  for varied functions and each in your passion initiatives and actual time initiatives.If any features happen in your house you’ll be able to invite your folks by saring the situation data with the assistance of a qr code.If you’re at workplace or faculty the identical could be utilized for occasion scheduling and kind filling.

Right here is the Github hyperlink.

Key Takeaways

  • QR codes are versatile instruments that retailer varied knowledge varieties, 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 data sharing.
  • Customizing QR codes with colours or logos can enhance branding and visible enchantment.
  • Wi-Fi QR codes present a seamless method to join gadgets to a community with out handbook entry.
  • Mastering QR Codes unlocks the potential to streamline knowledge sharing, improve consumer experiences, and enhance entry to data rapidly and successfully.

Steadily Requested Questions

Q1. What’s a QR code?

A. A QR code is a two-dimensional barcode that shops data equivalent to URLs, textual content, or Wi-Fi credentials, and could be scanned rapidly by a digital camera 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 together with your knowledge.

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

A. Sure, you’ll be able to change the foreground and background colours, and even embed logos inside the QR code for a extra personalised look.

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

A. A Wi-Fi QR code shops community credentials (SSID, password, and safety sort) 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, equivalent to sharing URLs, contact data, 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, equivalent to sharing URLs, Wi-Fi credentials, and extra, enhancing comfort and accessibility in digital interactions.

The media proven on this article is just not 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