S or /

SourceModule skia

Python bindings for the Skia Graphics Library.

This module provides Python wrappers around Skia's C++ API, so that you can draw text, geometry, and images from Python on desktop and mobile platforms.

Overview§

Skia is organized around the Canvas object. It is the host for the "draw" calls: draw_rect, draw_path, draw_text, etc. Each of these has two components: the primitive being drawn (Rect, Path, etc.) and color/style attributes (Paint).

canvas.draw_rect(rect, paint)

The paint holds much of the state describing how the rectangle (in this case) is drawn: what color it is, if it is filled or stroked, how it should blend with what was previously drawn.

The canvas holds relatively little state. It points to the actual pixels being drawn, and it maintains a stack of matrices and clips. Thus in the above call, the canvas' current matrix may transform the coordinates of the rectangle (translation, rotation, skewing, perspective), and the canvas' current clip may restrict where on the canvas the rectangle will be drawn, but all other stylistic attributes of the drawing are controlled by the paint.

Most Skia types are re-exported at the module root (for example Canvas, Surface, Paint, Path, Image), so you can usually write skia.Canvas without digging into the module tree.

Getting started§

A minimal example that draws into a raster surface and saves a PNG:

import skia

surface = skia.Surface(256, 256)
canvas = surface.canvas()
canvas.clear("ffffff")
paint = skia.Paint()
paint.antialias = True
paint.color = "000000"
canvas.draw_circle(128, 128, 100, paint)
surface.write_file("out.png")

Modules§

canvas

Canvas provides an interface for drawing, and how the drawing is clipped and transformed. Canvas contains a stack of skia.Matrix and clip values.

codec

Decoding of encoded images into skia.Images and skia.Pixmaps.

font

Font controls options applied when drawing and measuring text.

font_metrics

The metrics of a font. The metric values are consistent with the Skia y-down coordinate system.

font_style

Describes the style of a font: weight, width, and slant.

image

Describes a two dimensional array of pixels to draw. An Image is an immutable, thread-safe container for pixel data.

image_filter

Base class for image filters: when installed in a skia.Paint, drawing occurs as usual but as if into an offscreen, which is then filtered before being drawn to the device.

matrix

Holds a 3x3 matrix for transforming coordinates. This allows mapping Point2d and vectors with translation, scaling, skewing, rotation, and perspective.

paint

Paint controls options applied when drawing. Paint collects all options outside of the skia.Canvas clip and skia.Canvas matrix.

path

Describes geometry as a set of contours, each beginning with a move verb and followed by lines, quadratic beziers, conics, and cubic beziers.

path_builder

Helper to build a skia.Path incrementally by adding verbs, with a more convenient API than skia.Path itself.

path_types

Types used by skia.Path: fill type, direction, segment mask, and verb.

rrect

Describes a rounded rectangle with a bounds and a pair of radii for each corner. The bounds and radii can be set so that RoundRect describes: a rectangle with sharp corners; a circle; an oval; or a rectangle with one or more rounded corners.

sampling_options

Options for sampling images: filter mode, mipmap mode, and cubic resampler.

shader

Specifies the source color(s) for what is being drawn. If a skia.Paint has no shader, the paint's color is used.

surface

Describes a drawing destination: a Surface manages the pixels or GPU resources that a skia.Canvas draws into.

talonAlphaType, Canvas, Color, ColorType, Image
textlayoutTextAlign
typeface

Describes the typeface and style of a font; a Typeface is used by skia.Font to draw text.

Classes§

AddMode
AlphaType Enum

Describes how to interpret the alpha component of a pixel. A pixel may be opaque, or alpha, describing multiple levels of transparency.

ArcSize
Bitmap

Bitmap describes a two-dimensional raster pixel array. Bitmap is built on ImageInfo, containing integer width and height, ColorType and AlphaType describing the pixel format, and ColorSpace describing the range of colors. Bitmap points to PixelRef, which describes the physical array of pixels. ImageInfo bounds may be located anywhere fully inside PixelRef bounds.

BlendMode Enum

Blends are operators that take in two colors (source, destination) and return a new color. Many of these operate the same on all 4 components: red, green, blue, alpha. For these, we just document what happens to one component, rather than naming each one separately.

Canvas

Canvas provides an interface for drawing, and how the drawing is clipped and transformed. Canvas contains a stack of Matrix and clip values.

ClipOp Enum
Color

32-bit ARGB color value, unpremultiplied. Color components are always in a known order. This is different from SkPMColor, which has its bytes in a configuration dependent order, to match the format of BGRA 8888 color type bitmaps. Color is the type used to specify colors in skia.Paint and in gradients.

ColorType Enum

Describes how pixel bits encode color. A pixel may be an alpha mask, a grayscale, RGB, or ARGB.

CubicResampler

Specify b and c (each between 0...1) to create a shader that applies the corresponding cubic reconstruction filter to the image.

Data
EncodedImageFormat Enum

Enum describing format of encoded data.

FilterMode Enum
FilterQuality
Font

Font controls options applied when drawing and measuring text.

FontHinting
FontMetrics
FontSlant Enum
Fontstyle
Image

Image describes a two dimensional array of pixels to draw. The pixels may be decoded in a raster bitmap, encoded in a Picture or compressed data stream, or located in GPU memory as a GPU texture.

ImageFilter
ImageInfo

Describes pixel dimensions and encoding. skia.Bitmap, skia.Image, skia.Pixmap, and skia.Surface can be created from ImageInfo. ImageInfo can be retrieved from skia.Bitmap and skia.Pixmap, but not from skia.Image and skia.Surface. For example, skia.Image and skia.Surface implementations may defer pixel depth, so may not completely specify ImageInfo.

Matrix
Matrix4

4x4 matrix used by skia.Canvas and other parts of Skia.

MipmapMode Enum
Paint

Paint controls options applied when drawing.

PaintStyle
Path

Path contain geometry. Path may be empty, or contain one or more verbs that outline a figure. Path always starts with a move verb to a Cartesian coordinate, and may be followed by additional verbs that add lines or curves. Adding a close verb makes the geometry into a continuous loop, a closed contour. Path may contain any number of contours, each beginning with a move verb.

PathBuilder
PathDirection
PathFillType
PixelGeometry Enum

Description of how the LCD strips are arranged for each pixel. If this is unknown, or the pixels are meant to be "portable" and/or transformed before showing (e.g. rotated, scaled) then use PixelGeometry.UNKNOWN.

Point2d
Point3d
PointMode Enum

Selects if an array of points are drawn as discrete points, as lines, or as an open polygon.

Rect
RoundRect
SamplingOptions
Shader
StrokeCap Enum

Cap draws at the beginning and end of an open path contour.

StrokeJoin Enum

Join specifies how corners are drawn when a shape is stroked. Join affects the four corners of a stroked rectangle, and the connected segments in a stroked path.

Surface

Surface is responsible for managing the pixels that a canvas draws into. The pixels can be allocated either in CPU memory (a raster surface) or on the GPU (a RenderTarget surface). Surface takes care of allocating a Canvas that will draw into the surface. Call Surface.canvas() to use that canvas (but don't delete it, it is owned by the surface). Surface always has non-zero dimensions. If there is a request for a new surface, and either of the requested dimensions are zero, then None will be returned.

SurfaceProps

Describes properties and constraints of a given surface. The rendering engine can parse these during drawing, and can sometimes optimize its performance (e.g. disabling an expensive feature).

SurfacePropsFlags
TextAlign Enum
TextBlob
TileMode Enum

Describes how a shader draws outside of its original bounds: TileMode.CLAMP replicates the edge color, TileMode.REPEAT repeats the image horizontally and vertically, TileMode.MIRROR repeats the image with alternating mirror images so adjacent images always seam, and TileMode.Decal draws only within the original domain, returning transparent-black everywhere else.

Typeface

Functions§

Constants§

Re-exports§

AlphaType
Canvas
Color
ColorType
Image
Paint
Path
Rect
RoundRect
Shader
Surface