In this tutorial will explore how to generate barcode using Python.
Table of Contents
Barcode is one of the ways of representing data in a machine-readable format.
The concept of a barcode was originally developed in 1951 and was base on Morse code. The barcode format with vertical lines that we see everywhere now was developed in 1973.
The barcode success story began many years later when they started being used in the stores’ checkout systems to help cashiers automate getting the information for each product rather thank looking it up one by one.
Barcode is the predecessor of QR code, where the main differences between the two are the format and the amount the data it can store. Barcodes appear as rectangles with several vertical lines in it, whereas QR codes appear as squares with varying shapes inside of it. This level of complexity allows QR codes to store significantly more data than barcodes.
Now that we know what barcodes are and how they can be used. Let’s dive into the details of barcode formats and actually creating our first simple barcode using Python.
To continue following this tutorial we will need the following Python library: python-barcode.
If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
pip install python-barcode
The python-barcode library supports several barcode formats:
- Code 39
It includes uppercase letters, numeric digits, and some special characters. It can be of variable length up to the size of the barcode.
- Code 128
It includes uppercase letters, lowercase letters, numeric digits, and some special characters. It can be of variable length up to the size of the barcode.
It is a variant of Code 39 which encodes 6 digits + 1 check digit. This type of barcode is mostly used for pharmaceutical products in Germany.
It includes 12 digits + 1 check digit and is used to encode GTIN-13 (global trade item number) used mainly outside of North America.
It includes 7 digits + 1 check digit and is used to encode GTIN-8 (global trade item number) used mainly outside of North America.
JAN (Japanese Article Numbering) is a variant of EAN-13 where the first two digits must be 45 or 49 (Japan).
ISBN-13 (International Standard Book Number) is similar to EAN-13 with a special prefix.
ISBN-10 (International Standard Book Number) is a subtype of ISBN-13 which was use up to 31/12/2005.
ISSN (International Standard Serial Number) uses an 8 digit code to uniquely identify publications, magazines, and more.
UPC-A is a standard version of UPC (Universal Product Code) which consists of 12 digits.
It includes 14 digits and is used to encode GTIN-14 (global trade item number) used for traded goods.
It is a variant of Code 128 and is used for goods in commerce and industry. This type of barcode can include more than one data field.
There are a few more formats supported by the library. To check the complete list of supported formats using Python, you can run the following code:
import barcode barcode_formats = barcode.PROVIDED_BARCODES print(barcode_formats)
And you should get:
['codabar', 'code128', 'code39', 'ean', 'ean13', 'ean13-guard', 'ean14', 'ean8', 'ean8-guard', 'gs1', 'gs1_128', 'gtin', 'isbn', 'isbn10', 'isbn13', 'issn', 'itf', 'jan', 'nw-7', 'pzn', 'upc', 'upca']
Create a barcode using Python
In this section we will create a barcode using Python.
As an example, we use a UPC-A associated with the Coca-Cola can (12 oz.): 049000042511.
You can see this barcode on some of the Coca-Cola cans sold in the stores.
To get started let’s import the required dependencies:
import barcode from barcode.writer import ImageWriter
Next, we will define the barcode content, in our case the UPC number, which must be in a string format:
number = '049000042511'
Since we are going to create a UPC barcode, the appropriate barcode format needs to be referenced:
barcode_format = barcode.get_barcode_class('upc')
And now we can create the barcode and render it as a PNG file:
my_barcode = barcode_format(number, writer=ImageWriter()) my_barcode.save("generated_barcode")
You should see the generated_barcode.png appearing in the same directory as your main.py file which should be this barcode image:
Complete code for creating barcode using Python:
import barcode from barcode.writer import ImageWriter #Define content of the barcode as a string number = '049000042511' #Get the required barcode format barcode_format = barcode.get_barcode_class('upc') #Generate barcode and render as image my_barcode = barcode_format(number, writer=ImageWriter()) #Save barcode as PNG my_barcode.save("generated_barcode")
In this article we explored how to generate barcode using Python.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.