Package: pdf417
Easily encode your data into a 2D barcode using the PDF417 format.Licensed under the MIT License, seeLICENSE.InstallationInstall using pip:pip install pdf417CLIThepdf417gencommand can be used to generate a barcode from commandline. It
takes the input either as an argument or from stdin.# Show helppdf417genencode--help# Encode given text and display the barcodepdf417genencode"Beautiful is better than ugly"# Encode given text and save barcode to a file (extension determines format)pdf417genencode-obarcode.png"Explicit is better than implicit"# Input from a filepdf417genencode<input.txt# Piped inputpython-c"import this"|pdf417genencodeUsageCreating bar codes is done in two steps:Encode a string to a list of code words usingencode()Render the barcode using one of the rendering functions:render_image(),render_svg().Usage overview:frompdf417importencode,render_image,render_svg# Some data to encodetext="""Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated."""# Convert to code wordscodes=encode(text)# Generate barcode as imageimage=render_image(codes)# Pillow Image objectimage.save('barcode.jpg')# Generate barcode as SVGsvg=render_svg(codes)# ElementTree objectsvg.write("barcode.svg")Supports strings (unicode in py2) and byte arrays (str in py2):# These two inputs encode to the same code wordsencode(u"love 💔")encode(b"love\xf0\x9f\x92\x94")# Default encoding is UTF-8, but you can specify your ownencode(u"love 💔",encoding="utf-8")Encoding dataThe first step is to encode your data to a list of code words.encode(data,columns=6,security_level=2)ColumnsThe bar code size can be customized by defining the number of columns used to
render the data, between 1 and 30, the default value is 6. A bar code can have a
maximum of 90 rows, so for larger data sets you may need to increase the number
of columns to decrease the rows count.codes=encode(text,columns=12)image=render_image(codes)image.show()Security levelIncreasing the security level will produce stronger (and more numerous) error
correction codes, making the bar code larger, but less prone to corruption. The
security level can range from 0 to 8, and procuces2^(level+1)error
correction code words, meaning level 0 produces 2 code words and level 8
produces 512. The default security level is 2.codes=encode(text,columns=12,security_level=6)image=render_image(codes)image.show()Auto Numeric Compaction ModeThis mode can pack almost 3 digits (2.93) info a symbol character.
The words with length less 13 symbols will be invoked as Textcodes=encode(text,numeric_compaction=True)Render imageTherender_imagefunction takes the following options:scale- module width, in pixels (default: 3)ratio- module height to width ratio (default: 3)padding- image padding, in pixels (default: 20)fg_color- foreground color (default:#000000)bg_color- background color (default:#FFFFFF)NoteA module is the smallest element of a barcode, analogous to a pixel. Modules
in a PDF417 bar code are tall and narrow.The function returns a PillowImageobject containing the barcode.Colors can be specified as hex codes or using HTML color names.codes=encode(text,columns=3)image=render_image(codes,scale=5,ratio=2,padding=5,fg_color="Indigo",bg_color="#ddd")image.show()Render SVGTherender_svgfunction takes the following options:scale- module width, in pixels (default: 3)ratio- module height to width ratio (default: 3)padding- image padding, in pixels (default: 20)color- foreground color (default:#000000)The function returns aElementTreeobject containing the barcode in SVG format.Unlikerender_image, this function does not take a background color option.
The background is left transparent.codes=encode(text,columns=3)svg=render_svg(codes,scale=5,ratio=2,color="Seaweed")svg.write('barcode.svg')See alsopdf417-php- a PHP implementationgolang-pdf417- a Go implementationSpecifications- USS-PDF-417