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 Custom QR Code with Python Code

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to generate custom QR code with Python code. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

How is the ubiquitous QR code generated? Try making one yourself.

With the increasing popularity of the Internet and intelligent mobile devices, two-dimensional code (Quick Response code) has become one of the most widely used information carriers in the world. Tools for generating QR codes also emerge in endlessly, but most of them need to be done online, and the generated patterns are stereotyped and monotonous.

So is there any way to customize the generation of QR codes?

Recently, Arindom Bhattacharjee, an engineer and photographer who is keen on lifelong learning, wrote a custom method for generating QR codes, which takes only five lines of Python code. Interested readers can do it on their own.

5 lines of Python code custom generated QR code

The QR Code consists of black grid squares on a white background. Because it can store more information and has fast readability, it is more popular than bar codes.

Python provides a qrcode package that makes it easy to do this, starting with downloading the Python package:

Pip install pillowpip install qrcode

Then create a QR code that stores the previous article page link (https://towardsdatascience.com/face-detection-in-10-lines-for-beginners-1787aa1d9127) to see how simple the implementation process is:

Import qrcode

# Link for websiteinput_data = "https://towardsdatascience.com/face-detection-in-10-lines-for-beginners-1787aa1d9127"

# Creating an instance of qrcodeqr = qrcode.QRCode (version=1, box_size=10, border=5)

Qr.add_data (input_data) qr.make (fit=True)

Img = qr.make_image (fill='black', back_color='white') img.save ('qrcode001.png')

In this way, it takes only a few simple steps to create a QR code that can be linked to the previous article, as follows:

A quick scan through Wechat will take you to the previous article:

Specific parameters used

In the specific implementation process, the QRCode function in the above code requires the following parameters:

Version: defines the size of the generated QR code, which ranges from 1 to 40. The higher the value of this parameter, the larger the generated QR code image.

Box_size: defines the size of each pixel block

Border: defines the thickness of the border. For example, when its value is 5, it means that the border is as thick as five small blocks of pixels.

The add_data method is used to pass in the input text, which in this case is the hyperlink to the previous article. The make (fit=True) function ensures that the entire QR code is used, even if the input data can be stored in fewer blocks of pixels.

The final step is to convert it to an image file and store it. The make_image function can be used to specify the foreground color and background color in the image. Black and white are used in this QR code, but you can also change the color according to your personal preference. The save function stores the image in the current directory. Png file.

Python Library for generating QR codes: qrcode

The author's five-line code generation process is based on the qrcode library, which was launched by GitHub user sylnsfar in November 2016 and now has 7.4k star and 1.2k fork.

GitHub address: https://github.com/sylnsfar/qrcode

The library runs on Python 3 and can generate many types of QR codes, such as ordinary QR codes, artistic QR codes with pictures (black and white and color), and dynamic QR codes (black and white and color). It is more suitable for scenarios that are directly used to generate QR code pictures.

Let's just look at an example of how the library generates a QR code:

Ordinary QR code

Art QR code with picture

Dynamic QR code

About Python code how to generate custom QR code to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 0

*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

Internet Technology

Wechat

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

12
Report