Python3 integrated barcode and QR code

Python3 integrated barcode

installation:

pip install python-barcode

python-barcode supports barcode formats:

'code39', 
'ean', 
'ean13',
'ean8',
'gs1', 
'gtin', 
'isbn', 
'isbn10', 
'isbn13', 
'issn', 
'jan', 
'pzn', 
'upc', 
'upca'

Function demonstration: Use python-barcode to create ean13 standard barcode, and save the barcode as SVG format file

import barcode

# 设置条形码标准
ean = barcode.get('ean13', '123456789102')
print(ean.get_fullcode())
filename = ean.save('ean13')
print(filename)
options = dict(compress=True)
filename = ean.save('ean13', options)
print(filename)

Function demonstration: Use python-barcode to create ean13 standard barcode, and save the barcode as a png image

import barcode
from barcode.writer import ImageWriter

# 设置条形码标准
ean = barcode.get('ean13', '123456789102', writer=ImageWriter())
filename = ean.save('D:\\ean13')
print(filename)

python-barcode main barcode class

1. barcode.codex.Code39(code, write=None, add_checksum=True): Generate a Code39 standard barcode

      Parameter code: barcode content

      Parameter write: barcode format, the default is SVGWriter, that is, SVG format

      Parameter add_checksum: whether to add checksum, the default value is true

2. barcode.codex.PZN(pzn,write=None): Generate a PZN standard barcode

3. barcode.ean.EuropeanArticleNumber13(ean, write=None): Generate an EAN-13 standard barcode

4. barcode.ean.EuropeanArticleNumber8(ean, write=None): Generate an EAN-8 standard barcode

5. barcode.ean.JapanArticleNumber(jar, write=None): Generate a JAN standard barcode

6, barcode.isxn.InternationalStandarBookNumber13(isbn, write=None): Generate an ISBN-13 standard barcode

7, barcode.isxn.InternationalStandarBookNumber10(isbn, write=None): Generate an ISBN-10 standard barcode

8. barcode.isxn.InternationalStandarSerialNumber10(issn, write=None): Generate an ISSN standard barcode

9. barcode.upc.UniversalProductCodeA(upc, write=None, make_ean=False): Generate a UPC-A standard barcode

 

Python3 integrated QR code

installation:

pip install qrcode

Note: Before installing the qrcode library, you need to make sure that the Pillow library has been installed

Function demonstration: simple usage

import qrcode
img = qrcode.make("simpleqrcode")
img.save("D:\\simpleqrcode.jpg")
img.show()

Function demonstration: advanced usage

import qrcode
qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=10,)
qr.add_data('http://www.baidu.com/')
qr.make(fit=True)
img = qr.make_image()
img.show()
img.save('D:\\3.jpg')

Function demonstration: QR code with logo

from PIL import Image
import qrcode
 
qr = qrcode.QRCode(version=5,error_correction=qrcode.constants.ERROR_CORRECT_H,box_size=8,border=4)
qr.add_data("http://www.baidu.com/")
qr.make(fit=True)
 
img = qr.make_image()
img = img.convert("RGBA")
 
icon = Image.open("paste.jpg")
 
img_w,img_h = img.size
factor = 4
size_w = int(img_w / factor)
size_h = int(img_h / factor)
 
icon_w,icon_h = icon.size
if icon_w >size_w:
    icon_w = size_w
if icon_h > size_h:
    icon_h = size_h
icon = icon.resize((icon_w,icon_h),Image.ANTIALIAS)
 
w = int((img_w - icon_w)/2)
h = int((img_h - icon_h)/2)
icon = icon.convert("RGBA")
icon.show()
img.paste(icon,(w,h),icon)
img.show()
img.save('createlogo.jpg')

Detailed explanation of qrcode.QRCode class

QRCode contains the following four attribute parameters

version : An integer ranging from 1 to 40, which controls the size of the QR code (the minimum value is 1, which is a 12×12 matrix). If you want the program to determine it automatically, set the value to None and use the fit parameter.

error_correction : Control the error correction function of the QR code. Can take the following 4 constants.
     ERROR_CORRECT_L: About 7% or less of errors can be corrected.
     ERROR_CORRECT_M (default): About 15% or less of errors can be corrected.

     ERROR_CORRECT_Q: About 25% or less errors can be corrected.
     ROR_CORRECT_H: About 30% or less errors can be corrected.
box_size : Control the number of pixels contained in each small grid in the QR code.
border : Controls the number of grids contained in the border (the distance between the QR code and the picture border) (the default is 4, which is the minimum value specified by the relevant standards).

Guess you like

Origin blog.csdn.net/zhouzhiwengang/article/details/112797967