API Reference

Typograph Object

class typo_graphics.Typograph(*, glyph_images=None, samples=(3, 3), glyph_depth=2, typewriter=None, carriage_width=None)

Class for processing glyphs for the creation of images.

This class primarily is designed to be used to convert an image into a set of instructions, that can be typed on a typewriter to reproduce the image.

Class methods from_glyph_sheet() and from_directory() present other initialisation options.

Class attribues:
  • inbuilt_typewriters, list of inbuilt typewriters for which glyphs can be loaded.
  • glyph_sheet_paths, dictionary of paths to inbuilt typewriter glyph sheets.

Exposes image_to_text() , which can be used to convert any supplied image into glyph format.

Exposes following instance attributes:
  • glyphs, dictionary of typeable glyphs, keyed by glyph names, used in combinations.

  • standalone_glyphs, dictionary of typeable glyphs, keyed by glyph names, that are only to be used alone.

  • glyph_depth, integer detailing maximum glyphs that are combined together for each combination glyph.

  • sample_x, integer of samples across the glyph images.

  • sample_y, integer of samples down the glyph images.

  • samples, tuple of ints governing how glyphs are down-sampled for matching.

  • tree_sets, list of TreeSet objects containing all combination glyphs,

    and associated values.

__init__(*, glyph_images=None, samples=(3, 3), glyph_depth=2, typewriter=None, carriage_width=None)

Create Typograph object, optionally pass glyph images to use.

Defaults to using glyphs for the SR100 typewriter inbuilt glyph set.

Parameters:
  • glyph_images ({str: Image}) – dictionary of images, keyed with glyph names.
  • samples ((int, int) or int) – number of samples across and down, used to match glyphs to input images. If only int given, uses that value for both directions.
  • glyph_depth (int) – maximum number of glyphs to stack into single characters.
  • typewriter (str) – name of typewriter for which output is created. If glyph images are not provided, this name is used to look for an inbuilt typewriter’s glyph set. Valid values for which are given in Typograph.inbuilt_typewriters.
  • carriage_width (int) – maximum width of glyphs typeable on the typewriter carriage.
classmethod from_glyph_sheet(glyph_sheet, number_glyphs=None, glyph_dimensions=None, grid_size=None, glyph_names=None, spacing=None, **kwargs)

Create Typograph object with glyphs as extracted from glyph_sheet

Allows for a single Image to be used to provide glyph images.

Parameters:
  • glyph_sheet (Image or string or open file) – glyph sheet Image, to be split into glyphs, a filename for such image, or an open binary file object.
  • number_glyphs (int or None) – total number of glyphs present in glyph_sheet, if omitted, glyph_names must be present, and its length will be used.
  • glyph_dimensions ((int, int)) – pixel dimensions of glyphs given as (width, height).
  • grid_size ((int, int)) – if given, number of (rows, columns) that glyphs are arranged in.
  • glyph_names ([str]) – list of unique glyph names listed left to right, top to bottom.
  • spacing ((int, int)) – tuple of integer pixel spacing between adjacent glyphs, as number of pixels between glyphs horizontally and vertically.
  • kwargs – optional keyword arguments as for Typograph.
Returns:

An Typograph object using glyphs images extracted from glyph_sheet

Return type:

Typograph

Raises:
  • TypeError – if number_glyphs is not given.
  • TypeError – if neither grid_size or glyph_dimensions are specified.
  • ValueError – if duplicates in glyph_names
classmethod from_directory(glyph_directory, **kwargs)

Create Typograph object loading glyph images from a given directory.

In addition to images, the directory can contain a name_map.json file giving alias names for glyphs located in the directory.

Parameters:
  • glyph_directory (str) – A file path for directory containing glyph images.
  • kwargs – optional keyword arguments as for Typograph.
Returns:

An Typograph object using glyphs images found from directory.

Return type:

Typograph

add_glyph(glyph, use_in_combinations=False)

Add extra glyphs into the available pool.

New glyphs added in this manner can be excluded from use in combinations, to be used only as standalone glyph.

Adding a glyph already present in combinations, as a standalone will result in removal of glyph from combinations. The reverse of this is also true.

Parameters:
  • glyph (Glyph) – glyph to add.
  • use_in_combinations (bool) – use this glyph in combinations, default False.
remove_glyph(glyph, remove_from='Both')

Remove glyph from available pool.

Glyphs can be explicitly removed from combinations, standalone, or both.

Glyphs are removed by name, if passed a Glyph instance, will use the name attribute.

  • "Combinations" or "C" to remove from combinations
  • "Standalone" or "S" to remove from standalone glyphs
  • "Both" or "B" to remove from both

Returns the glyph instance removed, or None if the glyph was not found.

Parameters:
  • glyph (Glyph or str) – glyph to remove.
  • remove_from – string identifier for where to remove from.
Returns:

glyph removed or None.

Return type:

Glyph or None

image_to_text(image, max_size=(60, 60), cutoff=0, resize_mode=1, clip_limit=0.02, enhance_contrast=True, rescale_intensity=1.5, instruction_spacer=None, background_glyph=None, fit_mode='Scale')

Convert image into a glyph version, using the instance’s glyphs.

Parameters:
  • image (Image) – input Image to be processed and converted.
  • max_size ((int or None, int or None)) – maximum size for glyph version of image. Given as total number of glyphs able to be used across and down. If fit_mode is “Scale”, values of None in max_size are treated as infinite available space in that dimension. If (None, None), will match input image size to nearest whole glyph in each dimension.
  • fit_mode (string) – mode used to adjust image to fit within max_size. May be “Scale” to scale image to fit, or “Crop” to minimally crop image, maintaining center. “Crop” cannot be used with None values in max_size.
  • resize_mode – any resize mode as able to be used by resize().
  • clip_limit (float) – clip limit as used by equalize_adapthist().
  • enhance_contrast (bool) – enable or disable use of equalize_adapthist() on input image.
  • rescale_intensity (float, int or None) – control, or disable the effect of rescale_intensity(). Values higher than 1 cause values near the extremes, to be pushed into those extremes. A value lover than 1 will tend to move all values toward the average glyph value. If None is passed, the rescaling is skipped. This is preferred over passing unity. Defaults to expanding the output range 1.5 times.
  • cutoff (float) – cutoff level for near-enough glyph replacement. A value of 0.0 will permit no replacements.
  • instruction_spacer (Glyph) – glyph to be used to represent moving the typing position one step, without adding ink.
  • background_glyph (Glyph) – glyph to fill background of transparent image with.
Returns:

a TypedArt object, containing construction, output and instructions, after preprocessing.

Return type:

TypedArt

Glyph Object

class typo_graphics.Glyph(name, image, components=None, samples=(3, 3))

Represents the ink typed into one monospaced space.

This can comprise of multiple component glyphs, which are retained in components. In the case that the glyph is as typed, this will contain simply a reference to self.

Exposes following instance attributes:
  • name, the name of the glyph.
  • image, Image image of the glyph.
  • components, the typed keys that compose this glyph.
  • samples, tuple of ints governing how the glyph is down-sampled for matching.
  • fingerprint, scaled Image showing how glyph is internally processed.
  • fingerprint_display, rescaled version of fingerprint, to size of original image.

Explicitly supports summation with other glyph objects, which represent typing the two glyph atop one another.