Original link: http://www.juzicode.com/archives/6377
When visiting github, orange fungus found a fun Python library that can be used to make QR codes with background pictures or dynamic pictures. This library is also published on pypi and can be installed directly through pip:
python -m pip install amzqr
Import a module with the name "amzqr":
from amzqr import amzqr
The method is very simple. A run() method handles everything:
from amzqr import amzqr version, level, qr_name = amzqr.run( words, version=1, level='H', picture=None, colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd() )
The run() method has many input parameters. The meaning is as follows:
- words: text information to generate QR code, usually web address and other information;
- version: side length, ranging from 1 to 40. The larger the number, the larger the side length;
- Level: error correction level. The range is L, M, Q and H. H has the strongest error correction ability;
- Picture: path of background picture + file name;
- Colored: color, effective when the background picture is selected;
- Contrast: contrast, 1.0 indicates the original picture, smaller values indicate lower contrast, and larger values indicate the opposite. The default is 1.0
- Brightness: brightness. Its usage and value are the same as those of contrast
- save_name: the name of the file that generates the QR code. The format can be jpg, . png ,. bmp ,. gif, the default output file name is "qrcode.png";
- save_dir: save path for generating QR code pictures;
Let's take a look at the simplest example of QR code generation:
#VX official account: Orange code; juzicode.com from amzqr import amzqr version, level, qr_name = amzqr.run( words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A', version=1, level='H', picture=None, colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd() ) print(version, level, qr_name)
Operation results:
line 16: mode: byte 5 H E:\juzicode\qrcode.png
From the running results, although the size parameter version is set to 1, the returned version value is 5. Through the experiment, when the version is less than 5, the default generated image size is set to 5.
The generated QR code effect is shown in the following figure:
The QR code generated in this way is a little crude. Next, we generate a QR code to add a background picture. The background picture and py file are in the current working directory and the name is logo Jpg, the picture parameter passes in the name of the picture file. By changing the colorized parameter to False or True, you can generate two-dimensional codes with black-and-white or color pictures respectively:
#VX official account: Orange code; juzicode.com from amzqr import amzqr version, level, qr_name = amzqr.run( words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A', version=10, level='H', picture='logo.jpg', colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd() ) print(version, level, qr_name) version, level, qr_name = amzqr.run( words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A', version=10, level='H', picture='logo.jpg', colorized=True, contrast=1.0, brightness=1.0, save_name='logo-qrcode-color.png', save_dir=os.getcwd() ) print(version, level, qr_name)
Operation results:
line 16: mode: byte 10 H E:\juzicode\logo_qrcode.png line 16: mode: byte 10 H E:\juzicode\logo-qrcode-color.png
From the running results, it can be seen that save is not specified_ Name, the default generated file name is the background image file name+_ qrcode.png, the effect of the generated QR code is as follows:
Because of the strong anti-interference ability of QR code, it can also be recognized when adding background pictures.
Next, we generate a QR code with a dynamic graph background. The format of the dynamic graph is GIF, and the file name of the dynamic graph is also passed in through the picture variable:
#VX official account: Orange code; juzicode.com from amzqr import amzqr version, level, qr_name = amzqr.run( words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A', version=10, level='H', picture='dog.gif', colorized=True, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd() ) print(version, level, qr_name)
Operation results:
line 16: mode: byte 10 H E:\juzicode\dog_qrcode.gif
The generated QR code effect is shown in the following figure:
note1: amzqr( https://pypi.org/project/amzqr/ )It's myqr( https://pypi.org/project/MyQR/ )From the perspective of pypi's homepage, they both point to the same GitHub Library: https://github.com/x-hw/amazing-qr .
Note 2: gif is actually a collection of multiple static pictures. The QR code containing gif background is a collection of QR codes containing multiple static picture backgrounds.