1. ean13 standard bar code
from pystrich.ean13 import EAN13Encoder encode = EAN13Encoder('123123123123') encode.save('d:/barcode.png')
The most commonly used bar code ean13, the top three is the country code, followed by four corporate codes, followed by five corporate custom, the last digit is a check code
Parameter is a 12-bit value, the bar code 13 will generate the calculated check code is 10 - (3 * even bit sum sum odd bit +) 10% 123123123123X 10 - (. 1 + 2 + 3 + 2 + 1 + 3) * 3 + (3 + 1 + 1 + 2 + 3 + 2)% 10 -> 2
1231231231232
2. A simple two-dimensional code
. 1 Import qrcode 2 . 3 # production example two-dimensional code, can add text, figures, the URL . 4 QRC = qrcode.make (R & lt ' https://www.baidu.com ' ) . 5 # save the two-dimensional code . 6 qrc.save ( ' D: /qr.png ' )
3. The two-dimensional code added pictures
# Import Image image processing method of the PIL library from PIL Import Image # qrcode third-party libraries, installation requires pip install qrcode Import qrcode # Primary dimensional code image generated qr = qrcode.QRCode (version = None, error_correction = qrcode.constants. ERROR_CORRECT_H, box_size =. 8, border =. 4 ) # added two-dimensional code information, may be text, numbers, URL qr.add_data ( " http://www.besttang.com " ) # generates two-dimensional code example qr.make ( = Fit True) # the instance into images IMG = qr.make_image () # the color mode is converted into the RGBA IMG = img.convert ( " the RGBA " ) #Open logo file, two-dimensional code center logo image icon = Image.open ( " D: /favicon.jpg " ) # to obtain two-dimensional code width and height img_w, img_h = img.size factor = 4 # two-dimensional high code width calculated logo image width and height of the maximum size_w = int (img_w / factor) size_h = int (img_h / factor) # acquired width and height of the logo icon_w, icon_h = icon.size # Comparative logo maximum height and width width and height limits, and if it exceeds the maximum size of the logo will be adjusted to the maximum IF icon_w> size_w: icon_w = size_w IF icon_h> size_h: icon_h = size_h #Re-adjust the size of the logo Image.NEAREST: Low mass Image.BILINEAR: Bilinear # Image.BICUBIC: cubic spline interpolation Image.ANTIALIAS: high icon = icon.resize ((icon_w, icon_h), Image.ANTIALIAS) # the length and width of the logo image and logo determining the position of W = int ((img_w - icon_w) / 2 ) H = int ((img_h - icon_h) / 2 ) # convert logo color mode to the RGBA icon icon.convert = ( " the RGBA " ) # the logo image pasted to the specified position of the two-dimensional code img.paste (icon, (W, H)) # save the two-dimensional code to the specified location, provided the two-dimensional code format img.save ( ' D: / createlogo .png ' )
Definition:
Version: is an integer of 1 to 40, to control the size of the two-dimensional code (the minimum is 1, is a matrix of 12 × 12). If you want the program automatically determined, the value is set to None and used to fit parameters.
error_correction: control error correction features two-dimensional code. Possible values following four constants.
ERROR_CORRECT_L: about 7% or fewer errors can be corrected.
ERROR_CORRECT_M (default): about 15% or fewer errors can be corrected.
ROR_CORRECT_H: about 30% or fewer errors can be corrected.
box_size: controlling the number of pixels of each small two-dimensional code included in the lattice.
border: the border control (two-dimensional code and images from the border) grid contains a number of (default is 4, is the minimum specified in the relevant standard)