Issues Generating Barcode in data:image/png;base64 Format with Custom Size and No Text

I’m working on a Python project where my goal is to generate barcodes in the data:image/png;base64 format, without any human-readable footer text. Additionally, I need to adjust the size (height and width) and DPI (dots per inch) of the barcode, but I’m encountering some difficulties.

Specifically, I have two issues:

I cannot remove the human-readable footer text that appears below the barcode.
I am unable to customize the size (height, width) and DPI of the generated barcode.
I am using the python-barcode library along with the ImageWriter to generate the barcode image. I have tried using options like text=None to remove the text and various writer_options to control the size and resolution, but nothing seems to work as expected.

Here’s what I have tried so far:

import barcode
from barcode.writer import ImageWriter
from io import BytesIO
import base64

def generate_barcode_without_text(serial_no):
    barcode_instance = barcode.Code128(serial_no, writer=ImageWriter())
    
    writer_options = {
        'text': None,        # Disable text under the barcode
        'module_width': 0.2, # Adjust the width of the barcode
        'module_height': 15, # Adjust the height of the barcode
        'dpi': 300,          # Set the DPI for the image
        'quiet_zone': 6      # Set the quiet zone around the barcode
    }

    image_stream = BytesIO()
    barcode_instance.write(image_stream, writer_options=writer_options)

    image_stream.seek(0)
    barcode_base64 = base64.b64encode(image_stream.read()).decode('utf-8')

    return barcode_base64

The docs suggest to set the font_size to 0

https://python-barcode.readthedocs.io/en/stable/writers.html#common-writer-options

Font size of the text under the barcode in pt as integer. Font size zero suppresses text. Defaults to 10.

1 Like

While exploring similar barcode generation tasks, I was wondering, Is there a way to fully customize the font style and size of the human-readable text (if I decide to keep it) while still having complete control over the DPI and dimensions of the barcode? I’ve been experimenting with the python-barcode library, but I’m unsure if it allows this level of customization or if I should switch to another library.

Here’s a modified version of the code I’m using:

import barcode
from barcode.writer import ImageWriter
from io import BytesIO
import base64

def generate_custom_barcode(serial_no, text_visibility=True, font_size=10):
    barcode_instance = barcode.Code128(serial_no, writer=ImageWriter())
    
    writer_options = {
        'text': serial_no if text_visibility else None,  # Show or hide text
        'font_size': font_size,                          # Customize font size
        'module_width': 0.3,                             # Adjust barcode width
        'module_height': 20,                             # Adjust barcode height
        'dpi': 300,                                      # Set DPI
        'quiet_zone': 6                                  # Set quiet zone
    }

    image_stream = BytesIO()
    barcode_instance.write(image_stream, writer_options=writer_options)

    image_stream.seek(0)
    barcode_base64 = base64.b64encode(image_stream.read()).decode('utf-8')

    return barcode_base64

# Example usage
barcode_base64 = generate_custom_barcode("1234567890", text_visibility=True, font_size=12)
print(barcode_base64)

Has anyone managed to achieve precise text formatting or enhanced DPI control with this library, or is there another library that offers better flexibility for these features? Suggestions or tweaks would be much appreciated!