scistag.imagestag.canvas.Canvas¶
- class Canvas(size=None, target_image=None, default_color=Color(r=0.0, g=0.0, b=0.0, a=1.0, _rgba=(0.0, 0.0, 0.0, 1.0), _int_rgba=(0, 0, 0, 255)), image_format='RGB')[source]¶
Bases:
objectThe Canvas class provides functions for drawing graphical elements such as lines, circles or text into an Image’s pixel buffer.
- Parameters
size (
Union[tuple[float,float],Size2D,None]) – The size in pixels (if a new image shall be created)target_image (
Optional[Image]) – An image into which the canvas shall paintdefault_color (
Union[Color,tuple[int,int,int],tuple[int,int,int,int],tuple[float,float,float],tuple[float,float,float,float],str]) – The background fill colorimage_format (
PixelFormat) – The image format, currently only RGB, RGBA and G
Methods
Shifts the painting offset by given x, y distance in pixels
Clears the canvas
Clips the current painting region, relative to the current one
Draws given image onto the canvas
Returns the default font configured for this canvas
Tries to create a font handle for given font and returns it.
Loads an image and returns it
Repeats an image within the specified area as often as possible
Restores the previous state
Backups the current state
Draws a rectangle onto the canvas
Optimized rectangle drawing function for drawing a large amount of filled rectangles or frames in a single or multiple colors.
Shifts given coordinates by this canvas' current drawing offset
Renders a simple text using given parameters into the target image.
Returns an image representation of this canvas.
Attributes
__dict____doc____module____weakref__list of weak references to the object (if defined)
The canvas' size in pixels
The image's width in pixels
The image's height in pixels
The current painting offset in pixels
The min and max x y position valid for painting.
The rendering framework being used
Buffer to backup and restore the current painting state such as offset and clipping bounding
The image into which the canvas will paint
- add_offset_shift(offset)[source]¶
Shifts the painting offset by given x, y distance in pixels
- Parameters
offset – The distance of movement in pixels
- Returns
Self
- clear(color=Color(r=0.0, g=0.0, b=0.0, a=1.0, _rgba=(0.0, 0.0, 0.0, 1.0), _int_rgba=(0, 0, 0, 255)))[source]¶
Clears the canvas
- clip(offset, size)[source]¶
Clips the current painting region, relative to the current one
- Parameters
offset – The distance of movement in pixels
size – The width and height of the painting region
- get_default_font(size_factor=1.0, size=None, flags=None)[source]¶
Returns the default font configured for this canvas
- Parameters
size_factor – Factor by which the font shall be scaled
size (float | None) – The effective size in pixels. Overrides size_factor.
flags (set[str] | None) – The font flags. See
get_font().
- Return type
- Returns
The default font
- get_font(font_face, size, flags=None)[source]¶
Tries to create a font handle for given font and returns it.
- pattern(image, bounding, only_full_fit=False, **params)[source]¶
Repeats an image within the specified area as often as possible
- Parameters
image (
Image) – The image to repeatbounding (
Union[Bounding2D,tuple[int,int,int,int],tuple[float,float,float,float],tuple[Union[Pos2D,tuple[float,float]],Union[Pos2D,tuple[float,float]]],tuple[Union[Pos2D,tuple[float,float]],Union[tuple[float,float],Size2D]]]) – The bounding in which the image shall be repeatedonly_full_fit (
bool) – Defines if the pattern may only be repeated as long as the image fully fits into the areaparams – See
draw_image()for additional parameters
- Return type
- Returns
Self
- rect(pos=None, size=None, bounding=None, color=None, outline_color=None, outline_width=1)[source]¶
Draws a rectangle onto the canvas
- Parameters
pos (Pos2DTypes | None) – The position of the upper left edge
size (Size2DTypes | None) – The rectangle’s size in pixels
bounding (Bounding2DTypes | None) – The bounding of the rectangle (alternative to pos and size).
color (ColorTypes | None) – The inner color
outline_color (ColorTypes | None) – The outline color
outline_width (int) – The outline’s width
- Return type
- Returns
Self
- rectangle_list(rectangles, colors=None, single_color=None, outline_width=0)[source]¶
Optimized rectangle drawing function for drawing a large amount of filled rectangles or frames in a single or multiple colors.
Assumes raw-types for fast processing and can optimize the transformation process. This function is not (reasonable) faster if you just draw a single rectangle but should be preferred if you draw many ones.
- Parameters
rectangles (list[RawBoundingType]) – The list of rectangles ((x,y),(x2,y2))
colors (list[RawColorType] | None) – The list of colors (has to match the length of rectangles)
single_color (RawColorType | None) – The rectangle color (if all rectangles have the same color)
outline_width (int) – If defined a non-filled rectangles with given frame width will be painted
- Return type
- Returns
Self
- shift_position_by_offset(position)[source]¶
Shifts given coordinates by this canvas’ current drawing offset
- text(pos, text, color=Color(r=0.0, g=0.0, b=0.0, a=1.0, _rgba=(0.0, 0.0, 0.0, 1.0), _int_rgba=(0, 0, 0, 255)), font=None, h_align=HTextAlignment.LEFT, v_align=VTextAlignment.TOP, center=None, line_spacing=0, stroke_width=0, stroke_color=None, anchor=Anchor2D.TOP_LEFT, _show_formatting=False)[source]¶
Renders a simple text using given parameters into the target image.
- Parameters
pos (Pos2DTypes) – The text’s position in x, y coordinates
text (str) – The text to be drawn
color (ColorTypes) – The text’s color
font (Font) – The font to be used.
line_spacing (int) – The spacing between each line in pixels
h_align (HTextAlignmentTypes) –
The text’s horizontal alignment.
Note that the text will be horizontally aligned to the right of pos. If you want to center a text to the left and right of
posyou can either pass the argument center=true or set h_align to HTextAlignment.CENTER and anchor to Anchor2D.CENTER.v_align (VTextAlignmentTypes) – The text’s vertical alignment. (line-wise) Can NOT be used to center a multi-line text.
stroke_width (int) – The stroke width in pixels. Only has effect if stroke_color is not None
center (bool | None) – If set to true the text will be horizontally and vertically centered around pos.
stroke_color (ColorTypes | None) – The stroke color
anchor (Anchor2DTypes) – The positioning anchor relative to which the text shall be positioned. Can be used to center a multiline vertically.
_show_formatting (bool) – Defines if the formatting such as ascend and descent shall be visualized
- Return type
- Returns
Self
- to_image()[source]¶
Returns an image representation of this canvas.
If the canvas draws directly to a PIL image handle the returned image will not be a copy and point ot the current PIL image handle as well.
- Return type
- Returns
The image handle
- clip_region: tuple[float, float, float, float]¶
The min and max x y position valid for painting. Note that this is not respected by many paint commands but shall just help skipping irrelevant geometries completely.
- framework¶
The rendering framework being used
- height¶
The image’s height in pixels
- offset¶
The current painting offset in pixels
- size¶
The canvas’ size in pixels
- stat_stack: list[scistag.imagestag.canvas.CanvasState]¶
Buffer to backup and restore the current painting state such as offset and clipping bounding
- width¶
The image’s width in pixels