Skip to content

sci-bots/docket

Repository files navigation

Table of Contents generated with DocToc

Docket

Render text for stickers, labels, etc.

Docket (noun):

A writing on a letter or document stating its contents; any statement of particulars attached to a package, envelope, etc.; a label or ticket.


Usage

Render single string to PNG

>>> import docket
>>>
>>> width = 600  # Explicit width in pixels
>>>
>>> shape, surface = docket.render_text('hello, world!', width=width, font='Serif')
>>>
>>> with open('output.png', 'wb') as image_file:
>>>     surface.write_to_png(image_file)

The code above results in the following output:
Single string

Note: Any system font name may be specified, e.g., font='Arial'.

The optional fill and stroke arguments (as RGB color tuples) may be used to change the background fill color and text stroke color, respectively.

For example:

>>> shape, surface = docket.render_text('hello, world!', width=600,
...                                     fill=(1, 0, 0), stroke=(0, 0, 1))

results in the following output:
Fill and stroke options

Copy resulting surface image to system clipboard

>>> import docket.util
>>>
>>> docket.util.to_clipboard(surface)

Render list of strings

>>> import docket
>>>
>>> shape, surface = docket.render_text(['hello, world!', 'goodbye!'])

Render data table

>>> import docket
>>> import pandas as pd
>>>
>>>
>>> width = 250  # Explicit width in pixels
>>>
>>> df_data = pd.DataFrame([['Callie', 'Ernst'],
...                         ['Polly', 'Guerrero'],
...                         ['Mildred', 'Jones'],
...                         ['Tomasa', 'Rivera']],
...                         columns=['first_name', 'last_name'])
>>>
>>> shape, surface = docket.render_frame_text(df_data, width)

The code above results in the following output:
Data table

Specify width as pint quantity

Width/height may be specified as pint quantities.

For example:

>>> import docket
>>>
>>> # Fit to width of 20 mm (assuming 300 pixels per inch).
>>> width = 20 * docket.UREG.millimeter * 300 * docket.UREG.PPI
>>>
>>> shape, surface = docket.render_text('hello, world!', width=width)
>>> shape
<Quantity([ 236.22047244   72.        ], 'pixel')>
>>> shape.magnitude
array([ 236.22047244,   72.        ])

Install

The latest docket release is available as a Conda package from the sci-bots channel.

To install docket in an activated Conda environment, run:

conda install -c sci-bots -c conda-forge docket

License

This project is licensed under the terms of the BSD license


Contributors