The tmxlib.image modules

Image loading

The open() function provides a high-level interface to opening images, regardless

tmxlib.image.open(filename, trans=None, size=None)[source]

Open the given image file

Uses preferred_image_class.

Parameters:
  • filename – Name of the file to load the image from
  • trans – Optional color that should be rendered as transparent (this is not implemented yet)
  • size – Optional (width, height) tuple. If specified, the file will not be read from disk when the image size needs to be known. If and when the image is loaded, the given size is checked and an exception is raised if it does not match.
Returns:

An Image

Note that the file is not opened until needed. This makes it possible to use maps and tilesets that refer to nonexistent images.

tmxlib.image.preferred_image_class

The type of the object open() returns depends on the installed libraries. If Pillow (or PIL) is installed, the faster PilImage is used; otherwise tmxlib falls back to PngImage, which works anywhere but may be lower and only supports PNG files. Both wrappers offer the same API.

tmxlib.image.image_classes

A list of all available image classes, listed by preference. preferred_image_class is the first element in this list.

Image base classes

Image

class tmxlib.image_base.Image(data=None, trans=None, size=None, source=None)[source]

An image. Conceptually, an 2D array of pixels.

Note

This is an abstract base class. Use tmxlib.image.open() or tmxlib.image.preferred_image_class to get a usable subclass.

init arguments that become attributes:

data[source]

Data of this image, as read from disk.

size[source]

Size of the image, in pixels.

If given in constructor, the image doesn’t have to be decoded to get this information, somewhat speeding up operations that don’t require pixel access.

If it’s given in constructor and it does not equal the actual image size, an exception will be raised as soon as the image is decoded.

source

The file name of this image, if it is to be saved separately from maps/tilesets that use it.

trans

A color key used for transparency (currently not implemented)

Images support indexing (img[x, y]) as a shortcut for the get_pixel and set_pixel methods.

get_pixel(x, y)[source]

Get the color of the pixel at position (x, y) as a RGBA 4-tuple.

Supports negative indices by wrapping around in the obvious way.

set_pixel(x, y, value)[source]

Set the color of the pixel at position (x, y) to a RGBA 4-tuple

Supports negative indices by wrapping around in the obvious way.

Methods interesting for subclassers:

load_image()[source]

Load the image from self.data, and set self._size

If self._size is already set, assert that it equals

Note

It’s currently not possible to save modified images.

ImageRegion

class tmxlib.image_base.ImageRegion(image, top_left, size)[source]

A rectangular region of a larger image

init arguments that become attributes:

image

The “parent” image

top_left

The coordinates of the top-left corner of the region. Will also available as x and y attributes.

size

The size of the region. Will also available as width and height attributes.

Except for the constructor and attributes, ImageRegion supports the same external API as Image:

get_pixel(x, y)[source]

Get the color of the pixel at position (x, y) as a RGBA 4-tuple.

Supports negative indices by wrapping around in the obvious way.

set_pixel(x, y, value)[source]

Set the color of the pixel at position (x, y) to a RGBA 4-tuple

Supports negative indices by wrapping around in the obvious way.

class tmxlib.image_base.ImageBase[source]

Provide __getitem__ and __setitem__ for images

Pixel access methods with (x, y) pairs for position and (r, g, b, a) tuples for color.

Table Of Contents

Previous topic

The tmxlib.terrain module

Next topic

The tmxlib.helpers module

This Page