Writers

Common Writer Options

All writer take the following options (specified as keyword arguments to Barcode.save(filename, options) or set via Writer.set_options(options), where options is a dictionary where keys are option names and values are option values to be set).

Note

See the documentation of the specific writer for special options, only available for this writer.

module_width:

The width of one barcode module in mm as float. Defaults to 0.2.

module_height:

The height of the barcode modules in mm as float. Defaults to 15.0.

quiet_zone:

Distance on the left and on the right from the border to the first (last) barcode module in mm as float. Defaults to 6.5.

font_path:

Path to the font file to be used. Defaults to DejaVuSansMono (which is bundled with this package).

font_size:

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

text_distance:

Distance between the barcode and the text under it in mm as float. Defaults to 5.0.

background:

The background color of the created barcode as string. Defaults to white.

foreground:

The foreground and text color of the created barcode as string. Defaults to black.

center_text:

If true (the default) the text is centered under the barcode else left aligned.

Added in version 0.6.

Note

Some barcode classes change the above defaults to fit in some kind of specification.

BaseWriter

Both ImageWriter and SVGWriter are subclasses of BaseWriter:

class barcode.writer.BaseWriter(initialize: Callable | None, paint_module: Callable, paint_text: Callable | None, finish: Callable)

Baseclass for all writers.

Initializes the basic writer options. Child classes can add more attributes and can set them directly or using self.set_options(option=value).

Parameters:
  • initialize – Callback for initializing the inheriting writer. Is called: callback_initialize(raw_code)

  • paint_module – Callback for painting one barcode module. Is called: callback_paint_module(xpos, ypos, width, color)

  • paint_text – Callback for painting the text under the barcode. Is called: callback_paint_text(xpos, ypos) using self.text as text.

  • finish – Callback for doing something with the completely rendered output. Is called: return callback_finish() and must return the rendered output.

calculate_size(modules_per_line: int, number_of_lines: int) tuple

Calculates the size of the barcode in pixel.

Parameters:
  • modules_per_line – Number of modules in one line.

  • number_of_lines – Number of lines of the barcode.

Returns:

Width and height of the barcode in pixel.

packed(line: str) Generator[tuple[int, float], str, None]

Pack line to list give better gfx result, otherwise in can result in aliasing gaps ‘11010111’ -> [2, -1, 1, -1, 3]

This method will yield a sequence of pairs (width, height_factor).

Parameters:

line – A string matching the writer spec (only contain 0 or 1 or G).

register_callback(action: Literal['initialize', 'paint_module', 'paint_text', 'finish'], callback: Callable) None

Register one of the three callbacks if not given at instance creation.

Parameters:
  • action – One of ‘initialize’, ‘paint_module’, ‘paint_text’, ‘finish’.

  • callback – The callback function for the given action.

render(code: list[str])

Renders the barcode to whatever the inheriting writer provides, using the registered callbacks.

Parameters:
codeList

List consisting of a single string matching the writer spec (only contain 0 or 1 or G).

save(filename: str, output) str

Saves the rendered output to filename.

Parameters:
  • filename – Filename without extension.

  • output – The rendered output.

Returns:

The full filename with extension.

set_options(options: dict) None

Sets the given options as instance attributes (only if they are known).

Parameters:

options – All known instance attributes and more if the child class has defined them before this call.

SVGWriter

Renders barcodes as [optionally, compressed] SVG objects.

In addition to the common writer options you can give the following special option.

compress:

Boolean value to output a compressed SVG object (.svgz). Defaults to False

ImageWriter

Added in version 0.4b1.

Renders barcodes as image. Supports all the image formats supported by Pillow.

In addition to the common writer options you can give the following special options:

format:

The image file format as str. All formats supported by Pillow are valid (e.g. PNG, JPEG, BMP, …). Defaults to PNG.

dpi:

DPI as int to calculate the image size in pixel. This value is used for all mm to px calculations. Defaults to 300

Custom writers

It’s possible to create your own writer by inheriting from barcode.writer.BaseWriter.

In your __init__ method call BaseWriter’s __init__ and give your callbacks for:

  • initialize(raw_code)

  • paint_module(xpos, ypos, width, color)

  • paint_text(xpos, ypos)

  • finish()

Now instantiate a new barcode and give an instance of your new writer as argument. If you now call render on the barcode instance your callbacks get called.

Creating compressed SVGs

Saving a compressed SVG (SVGZ):

>>> import barcode
>>> ean = barcode.get('ean13', '123456789102')
# Now we look if the checksum was added
>>> ean.get_fullcode()
'1234567891026'
>>> filename = ean.save('ean13')
>>> filename
'ean13.svg'
>>> options = dict(compress=True)
>>> filename = ean.save('ean13', options)
>>> filename
'ean13.svgz'

Now you have ean13.svg and the compressed ean13.svgz in your current working directory. Open it and see the result.