Canvas
from zpui_lib.ui import Canvas
...
c = Canvas(o)
c.centered_text("Hello world", font=("Fixedsys62.ttf", 16))
c.display()
- class zpui_lib.ui.Canvas(o, base_image=None, name='', interactive=False)[source]
This object allows you to work with graphics on the display quicker and easier. You can draw text, graphical primitives, insert bitmaps and do other things that the
PILlibrary allows, with a bunch of useful helper functions.Args:
o: output devicebase_image: a PIL.Image to use as a base, if neededname: a name, for internal usageinteractive: whether the canvas updates the display after each drawing
- background_color = 'black'
default background color to use for drawing
- default_color = 'white'
default color to use for drawing
- width = 0
width of canvas in pixels.
- height = 0
height of canvas in pixels.
- size = (0, 0)
a tuple of (width, height).
- image = None
PIL.Imageobject theCanvasis currently operating on.
- load_font(path, size, alias=None, type='truetype')[source]
Loads a font by its path for the given size, then returns it. Also, stores the font in the
canvas.pyfont_cachedictionary, so that it doesn’t have to be re-loaded later on.Supports both absolute paths, paths relative to root ZPUI directory and paths to fonts in the ZPUI font directory (
ui/fontsby default).
- decypher_font_reference(reference)[source]
Is designed to detect the various ways that a
fontargument can be passed into a function, then return anImageFontinstance.
- point(coord_pairs, **kwargs)[source]
Draw a point, or multiple points on the canvas. Coordinates are expected in
((x1, y1), (x2, y2), ...)format, wherex*&y*are coordinates of each point you want to draw.Keyword arguments:
fill: point color (default: white, as default canvas color)
- line(coords, **kwargs)[source]
Draw a line on the canvas. Coordinates are expected in
(x1, y1, x2, y2)format, wherex1&y1are coordinates of the start, andx2&y2are coordinates of the end.Keyword arguments:
fill: line color (default: white, as default canvas color)width: line width (default: 0, which results in a single-pixel-wide line)
- text(text, coords, **kwargs)[source]
Draw text on the canvas. Coordinates are expected in (x, y) format, where
x&yare coordinates of the top left corner.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject or a tuple of(path, size), which are then supplied toCanvas.load_font().Do notice that order of first two arguments is reversed compared to the corresponding
PIL.ImageDrawmethod.Keyword arguments:
fill: text color (default: white, as default canvas color)
- vertical_text(text, coords, **kwargs)[source]
Draw vertical text on the canvas. Coordinates are expected in (x, y) format, where
x&yare coordinates of the top left corner.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject or a tuple of(path, size), which are then supplied toCanvas.load_font().Do notice that order of first two arguments is reversed compared to the corresponding
PIL.ImageDrawmethod.Keyword arguments:
fill: text color (default: white, as default canvas color)
- custom_shape_text(text, coords_cb, **kwargs)[source]
Draw text on the canvas, getting the position for each character from a supplied function. Coordinates are expected in (x, y) format, where
x&yare coordinates of the top left corner of the character.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject or a tuple of(path, size), which are then supplied toCanvas.load_font().Do notice that order of first two arguments is reversed compared to the corresponding
PIL.ImageDrawmethod.Keyword arguments:
fill: text color (default: white, as default canvas color)
- rectangle(coords, **kwargs)[source]
Draw a rectangle on the canvas. Coordinates are expected in
(x1, y1, x2, y2)format, wherex1&y1are coordinates of the top left corner, andx2&y2are coordinates of the bottom right corner.Keyword arguments:
outline: outline color (default: white, as default canvas color)fill: fill color (default: None, as in, transparent)
- rectangle_wh(coords, **kwargs)[source]
Draw a rectangle on the canvas. Coordinates are expected in
(x1, y1, w, h)format, wherex1&y1are coordinates of the top left corner, andw&hare width and height of the rectangle.Keyword arguments:
outline: outline color (default: white, as default canvas color)fill: fill color (default: None, as in, transparent)
This function calls
rectangleinternally.
- polygon(coord_pairs, **kwargs)[source]
Draw a polygon on the canvas. Coordinates are expected in
((x1, y1), (x2, y2), (x3, y3), [...])format, wherexXandyXare points that construct a polygon.Keyword arguments:
outline: outline color (default: white, as default canvas color)fill: fill color (default: None, as in, transparent)
- circle(coords, **kwargs)[source]
Draw a circle on the canvas. Coordinates are expected in
(xc, yx, r)format, wherexc&ycare coordinates of the circle center andris the radius.Keyword arguments:
outline: outline color (default: white, as default canvas color)fill: fill color (default: None, as in, transparent)
- ellipse(coords, **kwargs)[source]
Draw a ellipse on the canvas. Coordinates are expected in
(x1, y1, x2, y2)format, wherex1&y1are coordinates of the top left corner, andx2&y2are coordinates of the bottom right corner.Keyword arguments:
outline: outline color (default: white, as default canvas color)fill: fill color (default: None, as in, transparent)
- arc(coords, start, end, **kwargs)[source]
Draw an arc on the canvas. Coordinates are expected in
(x1, y1, x2, y2)format, wherex1&y1are coordinates of the top left corner, andx2&y2are coordinates of the bottom right corner.startandendangles are measured in degrees (360 is a full circle), start at 0 (3 o’clock) and increase clockwise.Arc angle alignment reference:
# 270 # 225 315 # 180 0 # 135 45 # 90
Keyword arguments:
fill: text color (default: white, as default canvas color)
- get_image(coords=None)[source]
Get the current
PIL.Imageobject. Ifcoordsare supplied, reeturns a rectangular region of the image, as defined bycoords.
- get_center(x=None, y=None)[source]
Get center coordinates. Will not represent the physical center - especially with those displays having even numbers as width and height in pixels (that is, the absolute majority of them).
You can substitute width and height of your choice as
xandy, purely so you can quickly center things in arbitrary areas.
- center_box(wb, hb, w=None, h=None, return_four=False)[source]
Get coordinates to center a (
wb,wh) box inside an area of (w,h). Basically, returns coordinates for the top left corner of a centered object.More or less, is a shorthand to get rid of some annoying math in app code.
- clear(coords=None, fill=None)[source]
Fill an area of the image with default background color. If coordinates are not supplied, fills the whole canvas, effectively clearing it. Uses the background color by default.
- check_coordinates(coords, check_count=True, rearrange_coords=True)[source]
A helper function to check and reformat coordinates supplied to functions. Currently, accepts integer coordinates, as well as strings - denoting offsets from opposite sides of the screen.
- check_coordinate_pairs(coord_pairs)[source]
A helper function to check and reformat coordinate pairs supplied to functions. Each pair is checked by
check_coordinates.
- centered_text(text, cw=None, ch=None, ox=0, oy=0, font=None)[source]
Draws centered text on the canvas. This is mostly a convenience function, used in some UI elements. You can pass alternate screen center values (
cw,ch) so that text is centered related to those, as opposed to the actual screen center.You can also pass offsets (
oxandoy) - for instance, passoy=-32to bring text 32 pixels upwards.
- get_text_bounds(text, font=None)[source]
Returns the uncompensated dimensions for a given text. If you use a non-default font, pass it as
font.
- get_text_bounds_compensated(text, font=None)[source]
Returns the compensated dimensions for a given text. If you use a non-default font, pass it as
font.
- get_centered_text_bounds(text, cw=None, ch=None, x=None, y=None, font=None)[source]
Returns the coordinates for the text to be centered on the screen. The coordinates come wrapped in a
Rectobject. If you use a non-default font, pass it asfont. You can also pass alternate screen center values so that text is centered related to those, as opposed to the real screen center.
- invert_rect(coords)[source]
Inverts the image in the given rectangle region. Is useful for highlighting a part of the image, for example.
- paste(image_or_path, coords=None, invert=False, mask='auto')[source]
Pastes the supplied image onto the canvas, with optional coordinates. Otherwise, you can supply a string path to an image that will be opened and pasted.
If
coordsis not supplied, the image will be pasted in the top left corner.coordscan be a 2-tuple giving the upper left corner or a 4-tuple defining the left, upper, right and lower pixel coordinate. If a 4-tuple is given, the size of the pasted image must match the size of the region.
- class zpui_lib.ui.MockOutput(width=None, height=None, type=None, device_mode=None, o=None, warn_on_display=True, hook=None)[source]
A mock output device that you can use to draw icons and other bitmaps using
Canvas.Keyword arguments:
widthheighttype: ZPUI output device type list (["b&w"]by default)device_mode: PIL device.mode attribute (by default,'1')
- zpui_lib.ui.open_image(path, *args, **kwargs)[source]
Simple wrapper around PIL.Image.open, that simplifies imports somewhat, and will allow us to improve upon it in the future.
- zpui_lib.ui.invert_image(path, *args, **kwargs)[source]
Simple wrapper around PIL.ImageOps.invert, that simplifies imports somewhat, and will allow us to improve upon it in the future.
- zpui_lib.ui.crop(image, min_width=None, min_height=None, align=None)[source]
Default crop alignment: top left. You can pass an argument to
align=to align it differently. You can pass a string like"right"/"bottom"/"hcenter"/"vcenter", or pass a list of strings, like["right", "vcenter"]."right"and"hcenter"arguments require you to specifymin_width, and"bottom"and"vcenter"arguments require you to specifymin_height.