2

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

1 Answer 1

1

I think the docs are slightly off for the current latest version 0.15-1.

I was able to change the size and remove the human readable barcode text from under the image with the following code:

def generate_barcode_without_text(serial_no):
    barcode_instance = barcode.Code128(serial_no, writer=ImageWriter())

    writer_options = {
        'write_text': False, # Disable text under the barcode
        'module_width': 0.8, # Adjust the width of the barcode
        'module_height': 25, # 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, options=writer_options)

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

    return barcode_base64

The option write_text seems to control whether the text under the barcode is written or not. The current docs don't mention this option and say setting the font_size to 0 suppresses the text, but this yields an error for me.

The parameter name for passing the writer options to the barcode class is options instead of writer_options too.

Here is the resulting barcode: example barcode for text "123456789"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.