pyglet.graphics.texture

exception TextureArrayDepthExceeded

Exception occurs when depth has hit the maximum supported of the array.

exception TextureArraySizeExceeded

Exception occurs ImageData dimensions are larger than the array supports.

class CompressedTexture(
width: int,
height: int,
handle: Any,
compression_format: CompressionFormat,
tex_type: TextureType = TextureType.TYPE_2D,
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
*,
key: TextureKey | None = None,
) → None

Base class for backend textures that store compressed image data on the GPU.

Concrete backends provide texture creation and compressed data uploads. Pixel readback and region views are not implemented by this base class.

Initialize texture metadata for an existing backend resource.

Parameters:
  • width (int) – Width of the texture in pixels.

  • height (int) – Height of the texture in pixels.

  • handle (Any) – Backend handle for the texture resource.

  • compression_format (CompressionFormat) – Compression format of the stored image data.

  • tex_type (TextureType) – Type of texture storage.

  • filters (TextureFilter | tuple[TextureFilter, TextureFilter] | None) – Minification and magnification filters. A single filter applies to both; None uses default_filters.

  • address_mode (AddressMode) – Addressing mode for texture coordinates.

  • anisotropic_level (int) – Requested anisotropic filtering level.

  • key (TextureKey | None) – Shared resource identity, or None to create a new identity.

key_type

alias of TextureKey

classmethod create_from_image(
image_data: CompressedImageData,
tex_type: TextureType = TextureType.TYPE_2D,
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
context: SurfaceContextT | None = None,
) → CompressedTexture

Create a compressed texture using a concrete backend implementation.

Return type:

CompressedTexture

classmethod supports_format(
fmt: CompressionFormat,
context: SurfaceContextT | None = None,
) → bool

Return whether the backend can upload this format in the given context.

Return type:

bool

get_image_data() → ImageData

Get an ImageData view of this image.

Changes to the returned instance may or may not be reflected in this image.

Return type:

ImageData

get_region(
x: int,
y: int,
width: int,
height: int,
) → _AbstractImage

Retrieve a rectangular region of this image.

Return type:

_AbstractImage

get_texture() → CompressedTexture

Return this texture.

Return type:

CompressedTexture

default_filters: TextureFilter | tuple[TextureFilter, TextureFilter] = (TextureFilter.LINEAR, TextureFilter.LINEAR)

Default minification and magnification filters, respectively.

images = 1
level: int = 0

The mipmap level of this texture.

target: int

The GL texture target (e.g., GL_TEXTURE_2D).

tex_coords = (0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0)

12-tuple of float, named (u1, v1, r1, u2, v2, r2, …). u, v, r give the 3D texture coordinates for vertices 1-4. The vertices are specified in the order bottom-left, bottom-right, top-right and top-left.

tex_coords_order: tuple[int, int, int, int] = (0, 1, 2, 3)

The default vertex winding order for a quad. This defaults to counter-clockwise, starting at the bottom-left.

x: int = 0
y: int = 0
z: int = 0
class PixelData(
width: int,
height: int,
format: ComponentFormat,
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
data: bytes | Array,
) → None

Typed pixel data read from GPU memory.

Unlike ImageData, this class does not perform image format conversions. Its data is stored as tightly packed native values of data_type.

to_image_data() → ImageData

Return an ImageData view of this pixel data.

No pixel conversion is performed. ImageData retains this object’s underlying buffer until it needs to convert or repack it.

Return type:

ImageData

data: bytes | Array
data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd']
format: ComponentFormat
height: int
property pitch: int

Number of bytes in one tightly packed row.

width: int
class PixelReadback(context: SurfaceContextT) → None

Base interface for backend-owned texture pixel readback resources.

Retain the backend context that owns the readback resources.

abstractmethod read_texture(
*args: Any,
**kwargs: Any,
) → tuple[int, int, Any]

Read one texture image or layer into backend-owned CPU memory.

Return type:

tuple[int, int, Any]

class Texture(
width: int,
height: int,
handle: Any,
tex_type: TextureType = TextureType.TYPE_2D,
internal_format: ComponentFormat = ComponentFormat.RGBA,
internal_format_size: int = 8,
internal_format_type: str = 'B',
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
*,
key: TextureKey | None = None,
context: SurfaceContextT | None = None,
) → None

An image loaded into GPU memory.

Typically, you will get an instance of Texture by accessing calling the get_texture() method of any AbstractImage class (such as ImageData).

Initialized in subclass.

key_type

alias of TextureKey

region_class

The class to use when constructing regions of this texture. The class should be a subclass of TextureRegion.

alias of TextureRegion

classmethod create(
width: int,
height: int,
tex_type: TextureType = TextureType.TYPE_2D,
internal_format: ComponentFormat = ComponentFormat.RGBA,
internal_format_size: int = 8,
internal_format_type: str = 'B',
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
blank_data: bool = True,
immutable: bool = False,
mipmap_levels: int = 1,
context: SurfaceContextT | None = None,
) → Texture

Create a Texture.

Create a Texture with the specified dimensions, target and format. On return, the texture will be bound.

Parameters:
  • width (int) – Width of texture in pixels.

  • height (int) – Height of texture in pixels.

  • tex_type (TextureType) – The type of texture.

  • internal_format (ComponentFormat) – The components of the internal format.

  • internal_format_size (int) – Bit size of each component in the internal format.

  • internal_format_type (str) – Internal format type, as a struct string value.

  • filters (TextureFilter | tuple[TextureFilter, TextureFilter] | None) – The texture filter for the min and mag filters. If a single value is passed, both values will be used as the filter.

  • address_mode (AddressMode) – The wrapping address mode of the texture.

  • anisotropic_level (int) – The anisotropic level of the texture.

  • blank_data (bool) – If True, initialize the texture data with all zeros. If False, do not pass initial data.

  • immutable (bool) – If True, allocate immutable-format texture storage.

  • mipmap_levels (int) – Number of mipmap levels to allocate. Immutable textures cannot add levels later.

  • context (TypeVar(SurfaceContextT, bound= SurfaceContext) | None) – If multiple contexts are being used, a specified context the texture is tied to.

Return type:

Texture

classmethod create_from_image(
image_data: ImageData | ImageDataRegion,
tex_type: TextureType = TextureType.TYPE_2D,
internal_format_size: int = 8,
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
context: SurfaceContextT | None = None,
) → Texture

Create a Texture from image data.

On return, the texture will be bound.

Parameters:
  • image_data (ImageData | ImageDataRegion) – The image instance.

  • tex_type (TextureType) – The type of texture.

  • internal_format_size (int) – The bit size of the internal format.

  • filters (TextureFilter | tuple[TextureFilter, TextureFilter] | None) – The texture filter for the min and mag filters. If a single value is passed, both values will be used as the filter.

  • address_mode (AddressMode) – The wrapping address mode of the texture.

  • anisotropic_level (int) – The anisotropic level of the texture.

  • context (TypeVar(SurfaceContextT, bound= SurfaceContext) | None) – If multiple contexts are being used, a specified context the texture will be tied to.

Return type:

Texture

bind(texture_unit: int = 0) → None

Bind to a specific Texture Unit by number.

Return type:

None

blit(
x: int,
y: int,
z: int = 0,
width: int | None = None,
height: int | None = None,
) → None

Blit the texture to the screen.

Removed as of 3.0. Instead, consider creating a Sprite with the Texture, and drawing it as part of a larger Batch.

Return type:

None

delete() → None

Delete this texture and the memory it occupies.

Textures are invalid after deletion, and may no longer be used.

Return type:

None

fetch(z: int = 0, level: int = 0) → ImageData

Fetch an image-oriented copy of this texture from the GPU.

Reading data back from the GPU can be a costly operation.

Modifying the returned ImageData object has no effect on the texture itself. Uploading ImageData back to the GPU/texture can be done with the upload() method.

The returned ImageData uses unsigned-byte image components. Floating-point and integer textures are unsupported here; use read_pixels() to retrieve their typed pixel values instead.

Parameters:
  • z (int) – For 3D textures, the image slice to retrieve.

  • level (int) – The mipmap level of the texture to retrieve.

Return type:

ImageData

generate_mipmaps() → None

Generate mipmaps for this texture from the base level.

Return type:

None

get_image_data(z: int = 0) → ImageData

Get the image data of this texture.

Return type:

ImageData

get_mipmapped_texture() → Texture
Return type:

Texture

get_region(
x: int,
y: int,
width: int,
height: int,
) → TextureRegion[Any]

Create a region sharing this texture’s storage.

Return type:

TextureRegion[Any]

get_texture() → Texture
Return type:

Texture

get_transform(
flip_x: bool = False,
flip_y: bool = False,
rotate: Literal[0, 90, 180, 270, 360] = 0,
) → TextureRegion[Any]

Create a texture region applying a simple transformation.

The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only; get_image_data() will fetch the untransformed data from the GPU.

Parameters:
  • flip_x (bool) – If True, the returned image will be flipped horizontally.

  • flip_y (bool) – If True, the returned image will be flipped vertically.

  • rotate (Literal[0, 90, 180, 270, 360]) – Degrees of clockwise rotation of the returned image. Only 90-degree increments are supported.

Return type:

TextureRegion[Any]

init_mipmaps(levels: int | None = None, blank_data: bool = True) → int

Initialize mipmap levels for this texture.

Parameters:
  • levels (int | None) – The total number of mipmap levels to initialize. If None, the maximum number of mipmap levels for this texture size is used.

  • blank_data (bool) – If True, initialize levels with zeroed data. If False, allocate the levels without initializing their contents.

Return type:

int

read_pixels(z: int = 0, level: int = 0) → PixelData

Read typed pixel values from this texture.

Floating-point and integer textures retain their numeric data type; normalized textures use unsigned-byte components. Unlike fetch(), this method does not provide general-purpose image conversion.

Parameters:
  • z (int) – For 3D or array textures, the image layer to retrieve.

  • level (int) – The mipmap level of the texture to retrieve.

Return type:

PixelData

upload(
image: ImageData | ImageDataRegion,
x: int,
y: int,
z: int,
level: int = 0,
) → None

Upload image data into the Texture at specific coordinates.

You must have this texture bound before uploading data.

The given x and y coordinates specify the lower-left pixel of the upload. If this texture is a 3D texture, the z parameter gives the image slice to upload into. The level parameter specifies the mipmap level to upload to.

Return type:

None

default_filters: TextureFilter | tuple[TextureFilter, TextureFilter] = (TextureFilter.LINEAR, TextureFilter.LINEAR)

The default minification and magnification filters, as a tuple. Both default to LINEAR. If a texture is created without specifying a filter, these defaults will be used.

property filters: tuple[TextureFilter, TextureFilter]

The current Texture filters.

Providing a single TextureFilter will adjust both minification and magnification filters. Otherwise, a tuple can be provided to adjust each individually.

images = 1
property mipmap_count: int

Return the number of mipmap levels initialized for this texture.

owner: Texture

The root texture that owns this texture’s storage.

A texture owns itself. Texture regions reference the texture that owns their underlying storage.

target: int

The GL texture target (e.g., GL_TEXTURE_2D).

tex_coords: tuple[float, float, float, float, float, float, float, float, float, float, float, float] = (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0)

12-tuple of float, named (u1, v1, r1, u2, v2, r2, …). u, v, r give the 3D texture coordinates for vertices 1-4. The vertices are specified in the order bottom-left, bottom-right, top-right and top-left.

tex_coords_order: tuple[int, int, int, int] = (0, 1, 2, 3)

The default vertex winding order for a quad. This defaults to counter-clockwise, starting at the bottom-left.

property uv: tuple[float, float, float, float]

Tuple containing the left, bottom, right, top 2D texture coordinates.

property valid_mipmaps: tuple[int, ...]

Return a sorted tuple of mipmap levels that currently have valid data.

x: int = 0
y: int = 0
z: int = 0
class Texture3D(
width: int,
height: int,
handle: Any,
tex_type: TextureType = TextureType.TYPE_2D,
internal_format: ComponentFormat = ComponentFormat.RGBA,
internal_format_size: int = 8,
internal_format_type: str = 'B',
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
*,
key: TextureKey | None = None,
context: SurfaceContextT | None = None,
) → None

A texture with more than one image slice.

Use the create_for_images() or create_for_image_grid() classmethod to construct a Texture3D.

region_class

alias of TextureRegion

item_height: int = 0
item_width: int = 0
items: list[TextureRegion] | tuple[TextureRegion, ...]
class TextureArray(
width: int,
height: int,
tex_id: int,
max_depth: int,
internal_format: ComponentFormat = ComponentFormat.RGBA,
internal_format_size: int = 8,
internal_format_type: str = 'B',
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
*,
context: SurfaceContextT | None = None,
) → None

A container of image layers, sampled with an array sampler.

Indexing selects a TextureArrayRegion suitable for a sprite. Regions of this texture remain array regions after cropping or transforming.

region_class

alias of TextureArrayRegion

classmethod create(
width: int,
height: int,
tex_type: TextureType = TextureType.TYPE_2D_ARRAY,
internal_format: ComponentFormat = ComponentFormat.RGBA,
internal_format_size: int = 8,
internal_format_type: str = 'B',
filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
address_mode: AddressMode = AddressMode.REPEAT,
anisotropic_level: int = 0,
blank_data: bool = False,
immutable: bool = False,
mipmap_levels: int = 1,
context: SurfaceContextT | None = None,
*,
max_depth: int = 256,
) → TextureArray[SurfaceContextT]

Create an empty texture array with capacity for max_depth layers.

Return type:

TextureArray[TypeVar(SurfaceContextT, bound= SurfaceContext)]

get_region(
x: int,
y: int,
width: int,
height: int,
) → TextureArrayRegion[SurfaceContextT]

Create a region sharing this texture’s storage.

Return type:

TextureArrayRegion[TypeVar(SurfaceContextT, bound= SurfaceContext)]

get_transform(
flip_x: bool = False,
flip_y: bool = False,
rotate: Literal[0, 90, 180, 270, 360] = 0,
) → TextureArrayRegion[SurfaceContextT]

Create a texture region applying a simple transformation.

The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only; get_image_data() will fetch the untransformed data from the GPU.

Parameters:
  • flip_x (bool) – If True, the returned image will be flipped horizontally.

  • flip_y (bool) – If True, the returned image will be flipped vertically.

  • rotate (Literal[0, 90, 180, 270, 360]) – Degrees of clockwise rotation of the returned image. Only 90-degree increments are supported.

Return type:

TextureArrayRegion[TypeVar(SurfaceContextT, bound= SurfaceContext)]

item_height: int
item_width: int
class TextureArrayRegion(
x: int,
y: int,
z: int,
width: int,
height: int,
owner: Texture,
) → None

A view of one array layer, sampled with an array sampler and its Z coordinate.

Initialized in subclass.

class TextureGrid(
texture: Texture | TextureRegion,
rows: int,
columns: int,
item_width: int | None = None,
item_height: int | None = None,
row_padding: int = 0,
column_padding: int = 0,
) → None

A texture containing a regular grid of texture regions.

To construct, create an ImageGrid first:

image_grid = ImageGrid(...)
texture_grid = TextureGrid(image_grid)

The texture grid can be accessed as a single texture, or as a sequence of TextureRegion. When accessing as a sequence, you can specify integer indexes, in which the images are arranged in rows from the bottom-left to the top-right:

# assume the texture_grid is 3x3:
current_texture = texture_grid[3] # get the middle-left image

You can also specify tuples in the sequence methods, which are addressed as row, column:

# equivalent to the previous example:
current_texture = texture_grid[1, 0]

When using tuples in a slice, the returned sequence is over the rectangular region defined by the slice:

# returns center, center-right, center-top, top-right images in that
# order:
images = texture_grid[(1,1):]
# equivalent to
images = texture_grid[(1,1):(3,3)]

Construct a grid for the given image.

You can specify parameters for the grid, for example setting the padding between cells. Grids are always aligned to the bottom-left corner of the image.

Parameters:
  • texture (Texture | TextureRegion) – A texture or region over which to construct the grid.

  • rows (int) – Number of rows in the grid.

  • columns (int) – Number of columns in the grid.

  • item_width (int | None) – Width of each column. If unspecified, is calculated such that the entire texture width is used.

  • item_height (int | None) – Height of each row. If unspecified, is calculated such that the entire texture height is used.

  • row_padding (int) – Pixels separating adjacent rows. The padding is only inserted between rows, not at the edges of the grid.

  • column_padding (int) – Pixels separating adjacent columns. The padding is only inserted between columns, not at the edges of the grid.

classmethod from_image_grid(
image_grid: ImageGrid,
) → TextureGrid
Return type:

TextureGrid

class TextureRegion(
x: int,
y: int,
z: int,
width: int,
height: int,
owner: Texture,
) → None

Common base for backend texture views sharing their owner’s storage.

Cropping and transforming a region preserve its concrete class, including the array-layer distinction represented by TextureArrayRegion.

Initialized in subclass.

delete() → None

Deleting a TextureRegion has no effect. Operate on the owning texture instead.

Return type:

None

fetch(z: int = 0, level: int = 0) → ImageDataRegion

Fetch the region. Its layer is fixed; z is ignored.

Return type:

ImageDataRegion

generate_mipmaps() → None

Generate mipmaps for this texture from the base level.

Return type:

None

get_image_data(z: int = 0) → ImageDataRegion

Get the image data of this texture.

Return type:

ImageDataRegion

get_region(x: int, y: int, width: int, height: int) → Self

Create a region sharing this texture’s storage.

Return type:

Self

get_transform(
flip_x: bool = False,
flip_y: bool = False,
rotate: Literal[0, 90, 180, 270, 360] = 0,
) → Self

Create a texture region applying a simple transformation.

The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only; get_image_data() will fetch the untransformed data from the GPU.

Parameters:
  • flip_x (bool) – If True, the returned image will be flipped horizontally.

  • flip_y (bool) – If True, the returned image will be flipped vertically.

  • rotate (Literal[0, 90, 180, 270, 360]) – Degrees of clockwise rotation of the returned image. Only 90-degree increments are supported.

Return type:

Self

init_mipmaps(levels: int | None = None, blank_data: bool = True) → int

Initialize mipmap levels for this texture.

Parameters:
  • levels (int | None) – The total number of mipmap levels to initialize. If None, the maximum number of mipmap levels for this texture size is used.

  • blank_data (bool) – If True, initialize levels with zeroed data. If False, allocate the levels without initializing their contents.

Return type:

int

read_pixels(
z: int = 0,
level: int = 0,
) → PixelData

Read typed pixels. The region’s layer is fixed; z is ignored.

Return type:

PixelData

upload(
image: ImageData | ImageDataRegion,
x: int,
y: int,
z: int,
level: int = 0,
) → None

Upload image data into the Texture at specific coordinates.

You must have this texture bound before uploading data.

The given x and y coordinates specify the lower-left pixel of the upload. If this texture is a 3D texture, the z parameter gives the image slice to upload into. The level parameter specifies the mipmap level to upload to.

Return type:

None

property mipmap_count: int

Return the number of mipmap levels initialized for this texture.

property valid_mipmaps: tuple[int, ...]

Return a sorted tuple of mipmap levels that currently have valid data.

class TextureSequence

Interface for a sequence of textures.

Typical implementations store multiple Texture to minimise state changes.

get_texture_sequence() → TextureSequence[TTexture]

Get a TextureSequence.

Added in version 1.1.

Return type:

TextureSequence[TypeVar(TTexture, bound= Texture)]

class UniformTextureSequence

Interface for a sequence of textures, each with the same dimensions.