GBTileset

The GBTileset class represents a GameBoy tileset. It is composed of GBTile (up to 255 tiles).

Creating a tileset from scratch:

from img2gb import GBTile, GBTileset

tileset = GBTileset()
tile = GBTile()

tileset.add_tile(tile)  # -> 0
tileset.length  # -> 1

Creating a tileset from a PIL image:

from img2gb import GBTileset
from PIL import Image

image = Image.open("./my_tileset.png")
tileset = GBTileset.from_image(image)
class img2gb.gbtileset.GBTileset[source]

Stores and manipulate a GameBoy tileset (up to 255 tiles).

add_tile(gbtile, dedup=False)[source]

Adds a tile to the tileset.

Parameters:
  • gbtile (GBTile) – The tile to add.
  • dedup (bool) – If True, the tile will be added only if there is no identical tile in the tileset (default = False).
Return type:

int

Returns:

The id of the tile in the tileset.

data

Raw data of the tiles in the tileset.

Type:list of int
classmethod from_image(pil_image, dedup=False, alternative_palette=False, sprite8x16=False)[source]

Create a new GBTileset from the given image.

Parameters:
  • pil_image (PIL.Image.Image) – The input PIL (or Pillow) image.
  • dedup (bool) – If True, deduplicate the tiles (default = False).
  • alternative_palette (bool) – Use the sprite’s alternative palette (inverted colors, default = False).
  • sprite8x16 (bool) – Rearrange the tiles to be used in 8x16 sprites (default = False).
Return type:

GBTileset

Note

  • The image width and height must be a multiple of 8.
  • The image can contain up to 255 different tiles.
length

Number of tiles in the tileset.

Type:int
merge(gbtileset, dedup=False)[source]

Merge the tiles of the given tileset in the current tileset.

Parameters:
  • gbtileset (GBTileset) – The tileset to merge into the current one.
  • dedup (bool) – Add only the tiles that are note already present in the current tileset (default = False).
tiles

Tiles of the tileset.

Type:GBTile
to_c_header_string(name='TILESET')[source]

Returns the C header (.h) code for the tileset.

Parameters:name (str) – The name of the variable in the generated code (always converted to uppercase in the generated code, default = "TILESET")
Return type:str

Example:

extern const UINT8 TILESET[];
define TILESET_TILE_COUNT 5
to_c_string(name='TILESET')[source]

Returns C code that represents the data of the tileset.

Parameters:name (str) – The name of the variable in the generated code (always converted to uppercase in the generated code, default = "TILESET")
Return type:str

Example:

const UINT8 TILESET[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xFF, 0x01, 0x81, 0x7F, 0xBD, 0x7F, 0xA5, 0x7B, 0xA5, 0x7B, 0xBD, 0x63, 0x81, 0x7F, 0xFF, 0xFF,
    0x7E, 0x00, 0x81, 0x7F, 0x81, 0x7F, 0x81, 0x7F, 0x81, 0x7F, 0x81, 0x7F, 0x81, 0x7F, 0x7E, 0x7E,
    0x3C, 0x00, 0x54, 0x2A, 0xA3, 0x5F, 0xC1, 0x3F, 0x83, 0x7F, 0xC5, 0x3F, 0x2A, 0x7E, 0x3C, 0x3C,
    0x04, 0x04, 0x04, 0x04, 0x0A, 0x0A, 0x12, 0x12, 0x66, 0x00, 0x99, 0x77, 0x99, 0x77, 0x66, 0x66,
};
to_hex_string()[source]

Returns the tileset as an hexadecimal-encoded string (one tile per line).

Return type:str

e.g.:

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
FF 01 81 7F BD 7F A5 7B A5 7B BD 63 81 7F FF FF
7E 00 81 7F 81 7F 81 7F 81 7F 81 7F 81 7F 7E 7E
3C 00 54 2A A3 5F C1 3F 83 7F C5 3F 2A 7E 3C 3C
04 04 04 04 0A 0A 12 12 66 00 99 77 99 77 66 66
to_image()[source]

Generates a PIL image from the tileset. The generated image is an indexed image with a 4 shades of gray palette.

Return type:PIL.Image.Image