diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-02-15 23:28:50 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-02-15 23:28:50 +0000 |
commit | 31418a90da67a1bcd1af0a39f33e8ecc5cdd2d49 (patch) | |
tree | 1127a547c91af3cb71912c6572a44c76484a1982 /tools/png2tiles.py | |
parent | 9c984aa67d2bd607032ba7fa911c77f9bbf87d4c (diff) | |
download | ubox-msx-lib-31418a90da67a1bcd1af0a39f33e8ecc5cdd2d49.tar.gz ubox-msx-lib-31418a90da67a1bcd1af0a39f33e8ecc5cdd2d49.zip |
Use black for formatting
Diffstat (limited to 'tools/png2tiles.py')
-rwxr-xr-x | tools/png2tiles.py | 103 |
1 files changed, 63 insertions, 40 deletions
diff --git a/tools/png2tiles.py b/tools/png2tiles.py index dd71de8..43eb1e0 100755 --- a/tools/png2tiles.py +++ b/tools/png2tiles.py @@ -33,20 +33,20 @@ DEF_H = 8 # TOSHIBA palette MSX_COLS = [ - (255, 0, 255), - (0, 0, 0), + (255, 0, 255), + (0, 0, 0), (102, 204, 102), (136, 238, 136), - (68, 68, 221), + (68, 68, 221), (119, 119, 255), - (187, 85, 85), + (187, 85, 85), (119, 221, 221), (221, 102, 102), (255, 119, 119), - (204, 204, 85), + (204, 204, 85), (238, 238, 136), - (85, 170, 85), - (187, 85, 187), + (85, 170, 85), + (187, 85, 187), (204, 204, 204), (238, 238, 238), ] @@ -55,15 +55,15 @@ MSX_COLS = [ def to_hex_list_str(src): out = "" for i in range(0, len(src), 8): - out += ', '.join(["0x%02x" % b for b in src[i:i + 8]]) + ",\n" + out += ", ".join(["0x%02x" % b for b in src[i : i + 8]]) + ",\n" return out def to_hex_list_str_asm(src): out = "" for i in range(0, len(src), 8): - out += '\tdb ' + ', '.join(["#%02x" % b for b in src[i:i + 8]]) - out += '\n' + out += "\tdb " + ", ".join(["#%02x" % b for b in src[i : i + 8]]) + out += "\n" return out @@ -71,7 +71,7 @@ def read_image(image_name, out, color): try: image = Image.open(image_name) except IOError: - raise IOError("failed to open the image \"%s\"" % image_name) + raise IOError('failed to open the image "%s"' % image_name) if image.mode != "RGB": raise Exception("not a RGB image") @@ -79,8 +79,9 @@ def read_image(image_name, out, color): (w, h) = image.size if w % DEF_W or h % DEF_H: - raise Exception("%s size is not multiple of tile size (%s, %s)" % - (image_name, DEF_W, DEF_H)) + raise Exception( + "%s size is not multiple of tile size (%s, %s)" % (image_name, DEF_W, DEF_H) + ) data = image.getdata() @@ -89,18 +90,20 @@ def read_image(image_name, out, color): for y in range(0, h, DEF_H): for x in range(0, w, DEF_W): # tile data - tile = [data[x + i + ((y + j) * w)] - for j in range(DEF_H) for i in range(DEF_W)] + tile = [ + data[x + i + ((y + j) * w)] for j in range(DEF_H) for i in range(DEF_W) + ] # get the attibutes of the tile # FIXME: this may not be right for i in range(0, len(tile), DEF_W): - cols = list(set(tile[i:i + DEF_W])) + cols = list(set(tile[i : i + DEF_W])) if len(cols) > 2: raise Exception( - "tile %d (%d, %d) has more than two colors: %r" % ( - ntiles, x, y, cols)) + "tile %d (%d, %d) has more than two colors: %r" + % (ntiles, x, y, cols) + ) elif len(cols) == 1: cols.append(MSX_COLS[1]) @@ -108,13 +111,13 @@ def read_image(image_name, out, color): if c not in MSX_COLS: raise Exception( "%s: tile %d (%d, %d) has a color not in the" - " expected MSX palette: %r" % (os.path.basename(image_name), - ntiles, x, y, c)) + " expected MSX palette: %r" + % (os.path.basename(image_name), ntiles, x, y, c) + ) # each tile has two color attributes per row color_idx[ntiles * DEF_H + i // DEF_W] = cols - color.append( - (MSX_COLS.index(cols[1]) << 4) | MSX_COLS.index(cols[0])) + color.append((MSX_COLS.index(cols[1]) << 4) | MSX_COLS.index(cols[0])) frame = [] for i in range(0, len(tile), 8): @@ -123,8 +126,9 @@ def read_image(image_name, out, color): for k in range(8): # 0 or 1 is determined by the order in the color attributes # for that row - byte |= color_idx[ - ntiles * DEF_H + i // DEF_W].index(tile[i + k]) << p + byte |= ( + color_idx[ntiles * DEF_H + i // DEF_W].index(tile[i + k]) << p + ) p -= 1 frame.append(byte) @@ -136,18 +140,35 @@ def read_image(image_name, out, color): def main(): - parser = ArgumentParser(description="PNG to MSX tiles", - epilog="Copyright (C) 2019 Juan J Martinez <jjm@usebox.net>", - ) + parser = ArgumentParser( + description="PNG to MSX tiles", + epilog="Copyright (C) 2019 Juan J Martinez <jjm@usebox.net>", + ) parser.add_argument( - "--version", action="version", version="%(prog)s " + __version__) - parser.add_argument("-i", "--id", dest="id", default="tileset", type=str, - help="variable name (default: tileset)") - parser.add_argument("-a", "--asm", dest="asm", action="store_true", - help="ASM output (default: C header)") - parser.add_argument("--no-colors", dest="no_colors", action="store_true", - help="don't include colors") + "--version", action="version", version="%(prog)s " + __version__ + ) + parser.add_argument( + "-i", + "--id", + dest="id", + default="tileset", + type=str, + help="variable name (default: tileset)", + ) + parser.add_argument( + "-a", + "--asm", + dest="asm", + action="store_true", + help="ASM output (default: C header)", + ) + parser.add_argument( + "--no-colors", + dest="no_colors", + action="store_true", + help="don't include colors", + ) parser.add_argument("image", nargs="+", help="image or images to convert") @@ -180,20 +201,22 @@ def main(): data_out = to_hex_list_str(out) print("#ifdef LOCAL") - print("const unsigned char %s[%d] = {\n%s\n};\n" % - (args.id, len(out), data_out)) + print( + "const unsigned char %s[%d] = {\n%s\n};\n" % (args.id, len(out), data_out) + ) if not args.no_colors: color_out = to_hex_list_str(color) - print("const unsigned char %s_colors[%d] = {\n%s\n};\n" % ( - args.id, len(color), color_out)) + print( + "const unsigned char %s_colors[%d] = {\n%s\n};\n" + % (args.id, len(color), color_out) + ) print("#else\n") print("extern const unsigned char %s[%d];" % (args.id, len(out))) if not args.no_colors: - print("extern const unsigned char %s_colors[%d];" % ( - args.id, len(color))) + print("extern const unsigned char %s_colors[%d];" % (args.id, len(color))) print("#endif // LOCAL\n") print("#endif // _%s_H\n" % args.id.upper()) |