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-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to use Python code to generate custom QR code, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

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?

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 pillow

Pip 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 website

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

# Creating an instance of qrcode

Qr = 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

After reading the above, have you mastered how to generate a custom QR code with Python code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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