current position:Home>[Python introduction project] use Python to generate QR code

[Python introduction project] use Python to generate QR code

2021-08-24 01:53:41 Hai Yong*

If you don't have a chance to do some interesting code , Beginners usually feel boring when they start programming . therefore , In this article , I use Python Made a simple small program to generate QR code

Use Python Generate qr code

This article is a series of articles , Three in total python Entry project . Beginners can try to implement these projects , And in Python Hands on operation in the compilation environment . There will be two made by you later Python Applet :

  • Use Tkinter Of Python The calendar GUI Applications
  • Use Python Convert image to pencil sketch

Let's start with the first one .

QR code represents fast response code . QR codes may look simple , But they can store a lot of data . No matter how much data it contains when scanning QR code , Users have immediate access to information . That's why they're called quick response codes .

In recent years, they have been used in many scenarios . It's on 1994 First appeared in Japan in . QR codes can be used to store ( code ) A lot of data and various types of data . for example , They can be used for :

Wechat payment 、 Alipay pay
Contact information
CSDN id、Weixin id wait .
Activity details
Web link
Product details
Link directly to download the app on the store .
They are also used for digital transactions by simply scanning QR codes .
By storing encryption details ( for example SSID、 Password and encryption type ) To visit Wi-Fi.

Etc., etc. , Very widely used

We just saw some advantages of QR code . Now we'll learn how to work in Python Generate two-dimensional code in .

For the use of python Generate QR code , We will use a name called QRcode Of python modular .

link : https://pypi.org/project/qrcode/

Use the following command to install it :

pip install qrcode

 Insert picture description here

We will generate a for CSDN Linked QR code , You can also try other . QR code generation is very simple . Just put the text 、 Link or anything passed to QRcode Modular “make” function .

import qrcode
img = qrcode.make("https://haiyong.blog.csdn.net")
img.save("haiyongQR.jpg")

Execute this code and the output is :

 Insert picture description here
You can scan and verify .

You can see that to generate this QR code, just 3 Line code . One more thing to mention is , You can also not provide a point qrcode.make() Function link , Can provide simple text .

for example :

️CSDN It is a platform to help developers grow . I love CSDN.️

Let's try :

import qrcode
img = qrcode.make("️CSDN It is a platform to help developers grow , I love CSDN️")
img.save("CSDN.jpg")

 Insert picture description here

This is a part , We generate a QR code and scan it . however , If I go the other way , We want to read this QR code , Now we want to know what is encoded in the QR code , Without scanning it ? So , We will use OpenCV.OpenCV It is a programming function library focusing on real-time computer vision tasks .

install opencv:

pip install opencv-python

 Insert picture description here

The code used to decode the QR code to understand the original string .

import cv2
d = cv2.QRCodeDetector()
val, _, _ = d.detectAndDecode(cv2.imread("CSDN.jpg"))
print("Decoded text is: ", val)

Output :

 Insert picture description here

wuhu ! take off !

Python In this QRcode The module provides many other functions . Try it yourself by reading the documentation . It will be fun and amazing for you .

There will be two made by you later Python Applet :

  • Use Tkinter Of Python The calendar GUI Applications
  • Use Python Convert image to pencil sketch

I've been writing a technology blog for a long time , And mainly through CSDN publish , This is my article Web Applet tutorial . I like to share technology and happiness through articles . You can visit my blog : https://haiyong.blog.csdn.net/ To learn more . I hope you will like !

You are welcome to put forward your opinions and suggestions in the comment area !

If you really learn something new from this article , Like it , Collect it and share it with your friends . Last , Don't forget or support .

copyright notice
author[Hai Yong*],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2021/08/20210824015339659i.html

The sidebar is recommended

guess what you like

Random recommended