image_prep

image_prep

Prepare images for treemap, simple SVG image editing and convertion to PNG

Functions

Name Description
size_colours Convert a list of sizes to colour hex codes
edit_svg_tree Set attributes of an SVG element tree
svg2png Converts an SVG element tree to a PNG
prep_svg Edits an SVG element tree and converts it to a PNG

size_colours

image_prep.size_colours(sizes, colormap='viridis', normalise=True)

Convert a list of sizes to colour hex codes

Converts sizes to colours using a matplotlib colour map, and returns the colours as hex codes. Optionally normalises the given sizes to 0-1 first.

Parameters

Name Type Description Default
sizes list[float] An iterable of numbers to convert to colours required
colormap str Name of a matplotlib colour map, or a colormap that accepts numbers 0-1. Defaults to “viridis”. 'viridis'
normalise bool Should sizes be normalised before converting to colours? True

Returns

Name Type Description
list[str] List of colour hex codes

edit_svg_tree

image_prep.edit_svg_tree(tree, **kwargs)

Set attributes of an SVG element tree

Parameters

Name Type Description Default
tree lxml.etree._ElementTree An SVG element tree required
**kwargs Any additional keyword arguments are used to update SVG attributes of all elements. E.g. fill, stroke, stroke_width. Note that _ are internally converted to -, e.g. stroke_width becomes stroke-width. {}

Returns

Name Type Description
lxml.etree._ElementTree An updated tree

svg2png

image_prep.svg2png(tree)

Converts an SVG element tree to a PNG

Parameters

Name Type Description Default
tree lxml.etree._ElementTree An SVG element tree required

Returns

Name Type Description
PIL.PngImagePlugin.PngImageFile PNG image

prep_svg

image_prep.prep_svg(tree, **kwargs)

Edits an SVG element tree and converts it to a PNG

Parameters

Name Type Description Default
tree lxml.etree._ElementTree An SVG element tree required
**kwargs Any additional keyword arguments are used to update SVG attributes of all elements. E.g. fill, stroke, stroke_width. Note that _ are internally converted to -, e.g. stroke_width becomes stroke-width. {}

Returns

Name Type Description
PIL.PngImagePlugin.PngImageFile PNG image