Bitmaps and Geometry

Rendering produces a novasvg.Bitmap; novasvg.Matrix, novasvg.Box and novasvg.Color are the small value types used for transforms, bounding boxes and colors.

Reference

class Bitmap(*args, **kwargs)
classmethod from_pil(pil)

A Bitmap from a Pillow image (a copy). Needs Pillow, imported only here.

L, LA, RGB and RGBA are taken as they are. Modes that hold 8-bit colour in another form are converted first: 1 -> L, P -> RGB (RGBA if the palette has transparency), PA / RGBa -> RGBA, La -> LA, RGBX / CMYK / YCbCr -> RGB. Other modes (I, I;16, F, LAB, HSV, …) have no 8-bit colour to take and raise TypeError: convert them yourself.

classmethod from_stb(image)

A Bitmap from a libstb.Image of 1 to 4 channels (L, LA, RGB, RGBA), a copy.

Needs libstb, imported only here. The pixels are taken as straight alpha.

clear(self, value: novasvg.Color) → None
clear(self, value: int) → None

Overloaded function.

  1. clear(self, value: novasvg.Color) -> None

Clear the bitmap with a stb::color.

  1. clear(self, value: int) -> None

Clear the bitmap with a packed 0xRRGGBBAA value.

convert_to_rgba(self) → None

Convert pixel data in place from ARGB32 Premultiplied to RGBA Plain. Only needed to hand the raw buffer to something else (e.g. numpy consumers wanting straight RGBA); NOT before write_to_*/to_*/write, which convert internally – doing both converts twice.

copy(self) → novasvg.Bitmap

An independent Bitmap that owns its own pixels (a deep copy).

is_null(self) → bool

Check if the bitmap is null.

numpy(self, *, rgba: bool = False, dtype: object | None = None, copy: bool = False) → object

The pixels as an (height, width, 4) ndarray.

By default a view of the Bitmap’s memory (no copy, writable): premultiplied BGRA on little-endian, until convert_to_rgba() is called. rgba=True returns a copy as ordinary straight RGBA instead (do not combine it with convert_to_rgba(), which already converts the buffer). copy=True copies the raw view. A dtype other than uint8 is always a copy.

to_bmp(self) → bytes

Encode the bitmap as BMP and return the file contents as bytes.

to_bytes(self, format: str = 'png', quality: int = 80) → bytes

Encode the bitmap in the given format (‘png’, ‘bmp’, ‘tga’, ‘jpg’/’jpeg’; a leading ‘.’ and any letter case are accepted) and return the file contents as bytes. quality (1-100) is used only for JPEG.

to_jpg(self, quality: int = 80) → bytes

Encode the bitmap as JPEG (quality 1-100) and return the file contents as bytes. JPEG has no alpha channel: render onto an opaque background first.

to_pil()

A Pillow image of mode RGBA with these pixels (a copy, straight alpha).

Needs Pillow, imported only here. Use this rather than PIL.Image.fromarray(bitmap): the Bitmap’s own memory is premultiplied BGRA, which Pillow would read as RGBA.

to_png(self) → bytes

Encode the bitmap as PNG and return the file contents as bytes (no temp file).

to_stb()

A libstb.Image with these pixels: 4 channels, straight RGBA, a copy.

Needs libstb (https://github.com/MohammadRaziei/libstb), imported only here. From there you can resize, convert, or write JPEG / PNG / BMP / TGA with stb’s encoders.

to_tga(self) → bytes

Encode the bitmap as TGA and return the file contents as bytes.

tobytes(self, *, rgba: bool = True) → bytes

The pixels as bytes, row-major (height, width, 4), tightly packed. A copy; needs no numpy. rgba=True (default): straight, un-premultiplied R, G, B, A. rgba=False: the raw memory (premultiplied BGRA on little-endian).

write(self, filename: str, jpg_quality: int = 80) → bool

Write the bitmap to a file, picking PNG/BMP/TGA/JPEG from the extension (.png/.bmp/.tga/.jpg/.jpeg; PNG if unrecognized). Returns True on success.

write_to_bmp(self, filename: str) → bool

Write the bitmap to a BMP file. Returns True on success.

write_to_jpg(self, filename: str, quality: int = 80) → bool

Write the bitmap to a JPEG file (quality 1-100). JPEG has no alpha channel: render onto an opaque background first. Returns True on success.

write_to_png(self, filename: str) → bool

Write the bitmap to a PNG file. Returns True on success.

write_to_tga(self, filename: str) → bool

Write the bitmap to a TGA file. Returns True on success.

from_array = <nanobind.nb_func object>
from_dlpack = <nanobind.nb_func object>
property height

Get the height of the bitmap.

property shape

(height, width, 4), the shape of the arrays this Bitmap exports.

property stride

Get the stride (bytes per row) of the bitmap.

property width

Get the width of the bitmap.

class Matrix(*args, **kwargs)
inverse(self) → novasvg.Matrix | None

The inverse matrix, or None when it is singular.

is_identity(self) → bool
rotate(self, radians: float) → novasvg.Matrix

Rotate by an angle in radians (applied first).

scale(self, sx: float, sy: float) → novasvg.Matrix

Scale (applied first).

shear(self, shx: float, shy: float) → novasvg.Matrix

Shear (applied first).

then(self, next: novasvg.Matrix) → novasvg.Matrix

This matrix applied first, then next.

translate(self, tx: float, ty: float) → novasvg.Matrix

Translate (applied first).

property a

Horizontal scaling factor.

property b

Vertical shearing factor.

property c

Horizontal shearing factor.

property d

Vertical scaling factor.

property e

Horizontal translation offset.

property f

Vertical translation offset.

rotation = <nanobind.nb_func object>
scaling = <nanobind.nb_func object>
shearing = <nanobind.nb_func object>
translation = <nanobind.nb_func object>
class Box(*args, **kwargs)
transform(self, matrix: novasvg.Matrix) → novasvg.Box

Transform the box by a matrix in place.

transformed(self, matrix: novasvg.Matrix) → novasvg.Box

Return a new transformed box.

property h

Height of the box.

property w

Width of the box.

property x

X-coordinate of the box.

property y

Y-coordinate of the box.

class Color(*args, **kwargs)
to_int(self) → int
value(self) → int

Get the color packed as a 0xRRGGBBAA integer.

property a

(self) -> int

property b

(self) -> int

black = <nanobind.nb_func object>
blue = <nanobind.nb_func object>
cyan = <nanobind.nb_func object>
from_hash = <nanobind.nb_func object>
from_name = <nanobind.nb_func object>
from_value = <nanobind.nb_func object>
property g

(self) -> int

green = <nanobind.nb_func object>
magenta = <nanobind.nb_func object>
orange = <nanobind.nb_func object>
property r

(self) -> int

red = <nanobind.nb_func object>
rgba = <nanobind.nb_func object>
transparent = <nanobind.nb_func object>
white = <nanobind.nb_func object>
yellow = <nanobind.nb_func object>