Generate QR code using Python
Mainly for Python3 beginner
By calling the MyQR interface, you can generate a personal QR code, and you can set the size of the QR code, whether to generate it based on existing pictures, and whether to generate a dynamic QR code.
Create environment
pip3 install MyQR
linux needs to download dependencies
http://labfile.oss.aliyuncs.com/courses/1126/libfreeimage-3.16.0-linux64.so
Ordinary QR code
from MyQR import myqr myqr.run('https://www.baidu.com')
[external link image transfer failed. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-UF7ybJvI-1654176813977)(C:\Users\lcx\Desktop\qrcode.png)]
Let's explain myqr in detail Parameters in the run() function
parameter | meaning | detailed |
---|---|---|
words | QR code pointing link | str, enter a link or sentence as a parameter |
version | Side length | int, control the side length. The range is 1 to 40. The larger the number, the larger the side length. The default side length depends on the length of the information you enter and the error correction level you use |
level | Error correction level | str, which controls the error correction level. The range is L, M, Q, h, which increases from left to right. The default error correction level is' H ' |
picture | Combined picture | str, combine QR QR QR code image with a picture under the same directory to generate a black-and-white picture |
colorized | colour | bool to change the generated picture from black and white to color |
contrast | contrast ratio | float to adjust the contrast of the picture. 1.0 means the original picture. A smaller value means a lower contrast. A larger value means the opposite. Default = 1.0 |
brightness | brightness | float to adjust the brightness of the picture. Other usage and values are the same as those of contrast |
save_name | Output file name | str, the default output file name is "qrcode.png" |
save_dir | Storage location | str, the default storage location is the current directory |
Art QR code with picture
myqr.run(words='https://www.shiyanlou.com', picture='1.jpg', save_name='artistic.png', )
[external link image transfer failed. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-yKBsZZgt-1654176813979)(C:\Users\lcx\Desktop\artistic1.png)]
myqr.run(words='https://www.baidu.com', picture='1.jpg', save_name='artistic.png', colorized=True, )
Art QR code with picture (True)
[external link image transfer failed. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ok28t13T-1654176813980)(C:\Users\lcx\Desktop\artistic.png)]
Dynamic QR code
myqr.run(words='https://www.baidu.com', picture='gakki.gif', save_name='artistic.gif', colorized=True, )
[external link image transfer failed. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-IHK9NAMJ-1654176813981)(C:\Users\lcx\Desktop\artistic.gif)]
qrcode │ LICENSE.md │ README.md │ requirements.txt #Environment dependent file | myqr.py | └───MyQR │ │ __init__.py │ │ myqr.py #Called file │ │ terminal.py #Setting parameters | | │ └───mylibs │ │ __init__.pt │ │ constant.py #Data analysis | | data.py #Data coding │ │ ECC.py #Error correction codes | | structure.py #data structure | | matrix.py #Get QR matrix | | draw.py #Generate QR code | | theqrmodule.py #Associative function │ └───example │ 0.png │ 1.png | 2.png | ...
Data analysis myqr/mylibs/constant py
Data code: myqr/mylibs/data py
Error correction code myqr/mylibs/ecc py
Construct the final data information myqr/mylibs/structure py + matrix. py
Create a matrix of QR codes
# MyQR/mylibs/matrix.py def get_qrmatrix(ver, ecl, bits): num = (ver - 1) * 4 + 21 qrmatrix = [[None] * num for i in range(num)] # Add finder mode and add separator add_finder_and_separator(qrmatrix) # Add calibration mode add_alignment(ver, qrmatrix) # Add time mode add_timing(qrmatrix) # Add blackened modules and reserved areas add_dark_and_reserving(ver, qrmatrix) maskmatrix = [i[:] for i in qrmatrix] # Place data bits place_bits(bits, qrmatrix) # Mask operation mask_num, qrmatrix = mask(maskmatrix, qrmatrix) # Format information add_format_and_version_string(ver, ecl, mask_num, qrmatrix) return qrmatrix
Generate QR code myqr/mylibs/draw py
def draw_qrcode(abspath, qrmatrix): unit_len = 3 x = y = 4*unit_len pic = Image.new('1', [(len(qrmatrix)+8)*unit_len]*2, 'white') #Create a new white underlay ''' The units in the circular matrix are enabled in the units that need to be blackened dra_a_black_unit()Functions are blackened. ''' for line in qrmatrix: for module in line: if module: draw_a_black_unit(pic, x, y, unit_len) #Draw black units x += unit_len x, y = 4*unit_len, y+unit_len saving = os.path.join(abspath, 'qrcode.png') pic.save(saving) # Save QR code picture return saving
How to merge pictures
/MyQR/myqr. The combine() method in. Py calls the pilot Library
Read picture operation
qr = Image.open(qr_name) #Read QR code picture qr = qr.convert('RGBA') if colorized else qr #Judge whether the QR code is colored bg0 = Image.open(bg_name).convert('RGBA') #Read pictures to merge bg0 = ImageEnhance.Contrast(bg0).enhance(contrast) # Adjust contrast bg0 = ImageEnhance.Brightness(bg0).enhance(brightness) # Adjust brightness
Overwrite the original QR code picture with the newly added picture, generate a new picture and save it.
for i in range(qr.size[0]-24): for j in range(qr.size[1]-24): if not ((i in (18,19,20)) or (j in (18,19,20)) or (i<24 and j<24) or (i<24 and j>qr.size[1]-49) or (i>qr.size[0]-49 and j<24) or ((i,j) in aligs) or (i%3==1 and j%3==1) or (bg0.getpixel((i,j))[3]==0)): qr.putpixel((i+12,j+12), bg.getpixel((i,j)))