Write 1D and 2D Barcodes to an Image - Python

This tutorial shows how to write barcodes onto an image and save the file in a Python application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to write barcodes onto an image and save the file using BarcodeEngine and BarcodeWriter classes in a Python application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Write 1D and 2D Barcodes to an Image - Python tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

This tutorial requires the following .NET DLLs:

For a complete list of which DLLs are required for specific barcode features, refer to Barcode Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Barcode Writer Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Project-Name.py and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Codecs") 
from Leadtools.Codecs import * 
LibraryLoader.add_reference("Leadtools.Barcode") 
from Leadtools.Barcode import * 

Add two new methods called write_upca_barcode(image) and write_qr_barcode(image). Call them from inside the main() method as shown below.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
 
    global barcode_engine 
    barcode_engine = BarcodeEngine() 
 
    image = RasterImage.Create(800, 800, 24, 300, RasterColor.White) 
     
    write_upca_barcode(image) 
    write_qr_barcode(image) 
    save_image(image, r"C:\temp\Write1Dand2DBarcodes.png") 

Add the below code to write UPCA and QR barcodes to a blank image and then save that image to file.

def write_upca_barcode(image): 
     
    data = BarcodeData() 
    data.Symbology = BarcodeSymbology.UPCA 
    data.Value = "01234567890" 
    data.Bounds = LeadRect(10, 10, 600, 200) 
 
    options = OneDBarcodeWriteOptions() 
    options.EnableErrorCheck = True 
    options.TextPosition = BarcodeOutputTextPosition.Default 
 
    barcode_engine.Writer.WriteBarcode(image, data, options) 
def write_qr_barcode(image): 
 
    data = QRBarcodeData() 
    data.SymbolModel = QRBarcodeSymbolModel.Model1AutoSize 
    data.Symbology = BarcodeSymbology.QR 
    data.Value = "QR Data Value" 
    data.Bounds = LeadRect(10, 250, image.ImageWidth, image.ImageHeight) 
     
    options = QRBarcodeWriteOptions() 
    options.GroupNumber = 0 
    options.GroupTotal = 0 
    options.XModule = 30 
    options.ECCLevel = QRBarcodeECCLevel.LevelL 
    options.HorizontalAlignment = BarcodeAlignment.Near 
    options.VerticalAlignment = BarcodeAlignment.Near 
 
    barcode_engine.Writer.WriteBarcode(image, data, options) 
def save_image(image, output_filename): 
 
    codecs = RasterCodecs() 
    codecs.Save(image, output_filename, RasterImageFormat.Png, 0) 
    print(f"The barcodes have been written and saved to {output_filename}") 

Note

This code saves the file to the C:\temp directory. Create the directory if it does not exist, or change the path to a valid folder.

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application will write the UPCA and QR barcodes to the blank RasterImage and save the image to the specified output file. The resulting RasterImage should look like the following image:

The application runs and writes the specified barcodes

Wrap-up

This tutorial showed how to create an application that writes 1D and 2D barcodes to a given image using BarcodeEngine and BarcodeWriter.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.