Skip to content

Commit f71ef30

Browse files
committed
Replace PyGame dependency with pillow
PyGame is used to load manipulate and save image data idependently of their file format. The PyGame project is fairly big and has caused various unexpected logging in the tools, so replace it with pillow, which is sufficient for our use case. Also expose OOP and functional API for tiletool, so it can be consumed programatically by 3rd customized tools.
1 parent c3d167e commit f71ef30

2 files changed

Lines changed: 584 additions & 460 deletions

File tree

tools/paltool.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2018-2019 Damien Ciabrini
2+
# Copyright (c) 2018-2026 Damien Ciabrini
33
# This file is part of ngdevkit
44
#
55
# ngdevkit is free software: you can redistribute it and/or modify
@@ -20,18 +20,10 @@
2020
Extract palette from a 2D image and convert it to C or ASM data.
2121
"""
2222

23-
from __future__ import print_function
24-
import struct
25-
import os
26-
import sys
2723
import argparse
28-
from random import randint
29-
30-
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
31-
os.environ['SDL_VIDEODRIVER'] = 'dummy'
32-
os.environ['SDL_AUDIODRIVER'] = 'dummy'
33-
import pygame
3424

25+
from PIL import Image
26+
from itertools import batched
3527

3628
def rgb24_to_packed15(col):
3729
r, g, b = [c >> 2 for c in col]
@@ -62,15 +54,18 @@ def main():
6254

6355
arguments = parser.parse_args()
6456

65-
img = pygame.image.load(arguments.FILE)
57+
img = Image.open(arguments.FILE)
6658

67-
# only keep the first 16 colors, even if palette has other
68-
# colors initialized but unused.
69-
pal = img.get_palette()[:16]
59+
# extract the palette as a 24bits RGB
60+
pal_ints = img.getpalette()
61+
pal_rgb = list(batched(pal_ints, 3))
62+
# make sure the palette has 16colors
63+
if len(pal_rgb) > 16:
64+
pal_rgb = pal_rgb[:16]
65+
elif len(pal_rgb) < 16:
66+
pal_rgb.extend([(0, 0, 0)] * (16 - len(pal_rgb)))
7067

71-
# for the time being, do not the 16th bit available in
72-
# the Neo Geo hardware
73-
ngpal = [rgb24_to_packed15(c) for c in pal]
68+
ngpal = [rgb24_to_packed15(c) for c in pal_rgb]
7469

7570
out = open(arguments.output, "w")
7671
output_c_anonymous_palette(ngpal, out)

0 commit comments

Comments
 (0)