GBTile

The GBTile class represents a single GameBoy tile (8x8 pixels, 16 Bytes).

Creating a tile from scratch:

from img2gb import GBTile

tile = GBTile()
tile.put_pixel(0, 0, 3)  # Put a black pixel at (0, 0)
tile.data  # -> [128, 128, 0, 0, 0, ...]
tile.to_hex_string()  # -> "80 80 00 00 00 ..."

Creating a tile from a PIL image:

from img2gb import GBTile
from PIL import Image

image = Image.open("./my_tile.png")
tile = GBTile.from_image(image)
class img2gb.gbtile.GBTile[source]

Stores and manipulates data of a single GameBoy tile (8x8 pixels).

property data

Raw data of the tile.

Type:

list of int

classmethod from_image(pil_image, tile_x=0, tile_y=0, alternative_palette=False)[source]

Create a new GBTile from the given image.

Parameters:
  • pil_image (PIL.Image.Image) – The input PIL (or Pillow) image.

  • tile_x (int) – The x location of the tile in the image (default = 0).

  • tile_y (int) – The y location of the tile in the image (default = 0).

  • alternative_palette (bool) – Use the sprite’s alternative palette (inverted colors, default = False).

Return type:

GBTile

get_pixel(x, y)[source]

Returns the color id of a pixel of the tile.

Parameters:
  • x (int) – The x coordinate of the pixel (0-7).

  • y (int) – The y coordinate of the pixel (0-7).

Return type:

int

put_pixel(x, y, color_id)[source]

Set the color of one of the tile’s pixels.

Parameters:
  • x (int) – The x coordinate of the pixel to change (0-7).

  • y (int) – The y coordinate of the pixel to change (0-7).

  • color_id (int) – the color of the pixel (0-3).

to_hex_string()[source]

Returns the tile as an hexadecimal-encoded string.

Return type:

str

e.g.:

"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 tile. The generated image is an indexed image with a 4 shades of gray palette.

Return type:

PIL.Image.Image