Welcome to Python-flavored Magnum! Please note that, while already being rather stable, this functionality is still considered experimental and some APIs might get changed without preserving full backwards compatibility.

magnum.ImageView2D class

Two-dimensional image view

Memory ownership and reference counting

Similarly to corrade.containers.ArrayView (and unlike in C++), the view keeps a reference to the original memory owner object in the owner field, meaning that calling del on the original object will not invalidate the view. Slicing a view creates a new view referencing the same original object, without any dependency on the previous view. That means a long chained slicing operation will not cause increased memory usage.

The owner is None if the view is empty.

Pixel data access

The class makes use of Python’s dynamic nature and provides direct access to pixel data in their concrete types via pixels. The returned views point to the underlying image data, element access coverts to a type corresponding to a particular PixelFormat and for performance-oriented access the view implements a buffer protocol with a corresponding type annotation.

Normalized formats (such as PixelFormat.RGB8_UNORM but also PixelFormat.RGBA8_SRGB) are unpacked to a corresponding floating-point representation in element access and packed from a floating-point representation in mutable acess. The type annotation is however still matching the original type (such as '3B' / '4B' in these cases), so code consuming these via the buffer protocol needs to handle the normalization explicitly if needed.

>>> data = array.array('B', [0xf3, 0x2a, 0x80, 0x23, 0x00, 0xff, 0x00, 0xff])
>>> image = ImageView2D(PixelFormat.RGBA8_SRGB, (2, 1), data)
>>> image.pixels[0, 0] # sRGB -> float conversion
Vector(0.896269, 0.0231534, 0.215861, 0.137255)
>>> np.array(image.pixels, copy=False)[0]
array([[243,  42, 128,  35],
       [  0, 255,   0, 255]], dtype=uint8)

Special methods

def __init__(self, arg0: PixelStorage, arg1: PixelFormat, arg2: Vector2i, arg3: corrade.containers.ArrayView, /) -> None
Constructor
def __init__(self, arg0: PixelFormat, arg1: Vector2i, arg2: corrade.containers.ArrayView, /) -> None
Constructor
def __init__(self, arg0: PixelStorage, arg1: PixelFormat, arg2: Vector2i, /) -> None
Construct an empty view
def __init__(self, arg0: PixelFormat, arg1: Vector2i, /) -> None
Construct an empty view
def __init__(self, arg0: Image2D, /) -> None
Construct a view on an image
def __init__(self, arg0: ImageView2D, /) -> None
Construct from any type convertible to an image view
def __init__(self, arg0: MutableImageView2D, /) -> None
Construct from a mutable view

Properties

data: corrade.containers.ArrayView get set
Raw image data
format: PixelFormat get
Format of pixel data
owner: object get
Memory owner
pixel_size: int get
Pixel size (in bytes)
pixels: corrade.containers.StridedArrayView2D get
Pixel data
size: Vector2i get
Image size
storage: PixelStorage get
Storage of pixel data

Method documentation

def magnum.ImageView2D.__init__(self, arg0: ImageView2D, /) -> None

Construct from any type convertible to an image view

Exceptions
RuntimeError If trade.ImageData2D.is_compressed is True

This function is used to implement implicit conversion from trade.ImageData2D in the trade module.