#!/bin/python3 import math import io from PIL import Image from consts import * from shared import main # convert a single DS tile (8x8) from packed palette index format to RGB colors def convert_chunk(img, chunk, offset): palette_indices = [] for byte in chunk: palette_indices += [ (byte >> 0) & 0x0f, (byte >> 4) & 0x0f, ] for i, palette_idx in enumerate(palette_indices): color = PICTOCHAT_PALETTE[palette_idx] location = ( i % DS_TILE_SIZE + offset[0], i // DS_TILE_SIZE + offset[1], ) img.putpixel(location, color) # convert a string of tiles into a PNG image def pc2png(data): tile_count = math.floor(len(data) / TILE_BYTES) size = ( TILES_HORIZONTAL * DS_TILE_SIZE, math.ceil(tile_count / TILES_HORIZONTAL) * DS_TILE_SIZE, ) img = Image.new(mode="RGB", size=size) for tile in range(tile_count): chunk_idx = tile * TILE_BYTES chunk = data[chunk_idx:chunk_idx+TILE_BYTES] tile_offset = ( tile % TILES_HORIZONTAL, tile // TILES_HORIZONTAL, ) convert_chunk(img, chunk, [x * DS_TILE_SIZE for x in tile_offset]) output = io.BytesIO() img.save(output, format='PNG') return output.getvalue() if __name__ == "__main__": main("bin", "png", pc2png)