Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate and read QR Code by Python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how Python generates and reads QR codes". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to generate and read QR codes in Python" can help you solve the problem.

The two-dimensional code is a black-and-white figure which is distributed in the plane (two-dimensional direction) according to a certain rule with a certain geometric figure, and records the information of data symbols.

QR codes, called fast response codes, may seem simple, but they can store large amounts of data. No matter how much data is contained in the scan of the QR code, the user can access the information immediately.

QR codes have also become rapidly popular in recent years and have become a part of our lives. There are many application scenarios for QR codes:

Access to information (business cards, maps, WIFI passwords, materials)

Website jump (jump to Weibo, mobile website, website)

Advertising push (users scan the code and browse the video and audio ads pushed by the merchants directly)

Mobile e-commerce (users scan code, mobile phone direct shopping to place orders)

Anti-counterfeiting traceability (users can scan the code to view the place of production; at the same time, the background can obtain the final place of consumption)

Discount promotion (users scan code, download electronic coupons, lucky draw)

Membership management (access to electronic membership information and VIP services on the user's mobile phone)

Mobile payment (scan the QR code of the goods and complete the payment through the mobile channel provided by the bank or third-party payment)

Account login (scan the QR code to log in to each website or software)

Python is also very easy to deal with QR codes. Today we will look at how to generate or read QR codes using a single line of code.

Generate a QR code

Python has a module for dealing with QR codes called qrcode. To generate QR codes, we need to install this library:

To generate the QR code is to call the make function of this module:

Import qrcodeimg = qrcode.make ('https://www.zhihu.com/people/wu-huan-bu-san')img.save('./pic.jpg')

Run this code and you will get the following QR code:

You can scan the code to have a try, there may be a surprise!

You must say: liar! This is not a line of code!

Well, these two lines can be merged:

Qrcode.make ('https://www.zhihu.com/people/wu-huan-bu-san').save('./pic.jpg') reads the QR code

Reading the QR code is to parse the hidden information behind the QR code. At this time, instead of using the qrcode module, we use the OpenCV module. I believe that readers of the official account must be familiar with this library and often appear in our articles.

Install the library first:

Pip install opencv-python

Next, let's take the QR code generated above as an example to take a look at the read code:

Import cv2d = cv2.QRCodeDetector () val, _, _ = d.detectAndDecode (cv2.imread ("pic.jpg")) print ("the secret is:", val) supplement

Of course, Python can generate not only static QR codes, but also dynamic

The following is an example code that implements a dynamic QR code, which you can refer to

#-*-coding:utf-8-*-# @ Time: 2021-09-3 picture='./sohucs.gif', colorized=True @ Author: carl_DJfrom MyQR import myqr# defines the content of the QR code word = "miss U" # append background myqr.run (word, # image type is bit .gif picture='./sohucs.gif', colorized=True, version=6)

Of course, Python can also make a custom QR code generation tool, which can generate black-and-white or color QR codes according to their own needs.

From gooey import GooeyParser,Gooeyfrom MyQR import myqr@Gooey (encoding='utf-8',program_name=' QR code generator', program_description=' generates dynamic, color, black and white QR codes', language='chinese') def main (): parser = GooeyParser (description= "generate dynamic, color, black and white QR codes") parser.add_argument ('text', widget= "TextField") parser.add_argument ('file') Widget= "FileChooser") # File selection box parser.add_argument ('style selection', widget='Dropdown',choices= {"black and white": "1", "color": 2, "dynamic": 3}) parser.add_argument ('file save path', widget='DirChooser') args = parser.parse_args () if args. Style selection = = 'black and white': # generate the QR code myqr.run (words=args. Text, version=9,save_name='qr.png',save_dir=args. File save path) print (successful) elif args. Style selection = = 'color': if args. File. Split ('.') [- 1] = 'gif' or args. File. Split ('.') [- 1] = 'png' or args. File .split ('.') [- 1] = 'jpeg': myqr.run (words=args. Text, version=9, save_name='qr.png', save_dir=args. File save path, picture=f' {args. File}', colorized=True) print ('success') elif args. Style selection = = 'dynamic': if args. File .split ('.') [- 1] = 'gif': myqr.run (words=args. Text, version=9, save_name='qrcode.gif', save_dir=args. File save path, picture=args. File, colorized=True) else: print ("the picture format is incorrect, the dynamic QR code picture must be in gif format") if _ _ name__ ='_ _ main__': main () this is the end of the introduction about "how to generate and read the QR code by Python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 274

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report