The tmxlib.image modules

Image loading

The open() function provides a high-level interface to reading images.

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 loaded as transparent

    Note

    Currently, loading images that use color-key transparency is very inefficient. If possible, use the alpha channel instead.

  • 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 Base

class tmxlib.image_base.ImageBase[source]

Image base class

This defines the basic image API, shared by Image and ImageRegion.

Pixels are represented as (r, g, b, a) float tuples, with components in the range of 0 to 1.

__getitem__(pos)[source]

Get a pixel or region

With a pair of integers, this returns a pixel via get_pixel():

Parameters:pos – pair of integers, (x, y)
Returns:pixel at (x, y) as a (r, g, b, a) float tuple

With a pair of slices, returns a sub-image:

Parameters:pos – pair of slices, (left:right, top:bottom)
Returns:a ImageRegion

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

Note

Currently, loading images that use color-key transparency is very inefficient. If possible, use the alpha channel instead.

Images support indexing (img[x, y]); see tmxlib.image_base.ImageBase.__getitem__()

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.

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

ImageRegion

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

A rectangular region of a larger image

init arguments that become attributes:

parent

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.

Table Of Contents

Previous topic

The tmxlib.terrain module

Next topic

The tmxlib.canvas and draw modules

This Page