Files
eo-gfx-extract/to_png.py
T
2026-05-30 20:14:46 +00:00

58 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Convert extracted .bmp sprites to .png with the black background made
transparent (colorkey transparency).
The EGF bitmaps store no usable alpha channel (alpha bytes are all zero);
transparency is by colorkey, and the background is pure black RGB(0,0,0).
This loads each BMP as RGB (ignoring the junk alpha), sets every pure-black
pixel to fully transparent, and writes a PNG.
Usage:
python3 to_png.py [dir ...] # default: all gfx*_extracted dirs
python3 to_png.py --key RRGGBB ... # custom colorkey (hex), default 000000
python3 to_png.py --keep # keep .bmp files (default keeps them)
"""
import sys, os, glob
from PIL import Image
def convert(path, key):
im = Image.open(path).convert('RGB') # force RGB, drop bogus alpha
im = im.convert('RGBA')
px = im.getdata()
kr, kg, kb = key
out = [(r, g, b, 0) if (r, g, b) == (kr, kg, kb) else (r, g, b, 255)
for (r, g, b, a) in px]
im.putdata(out)
dst = os.path.splitext(path)[0] + '.png'
im.save(dst)
return dst
def main():
args = sys.argv[1:]
key = (0, 0, 0)
dirs = []
i = 0
while i < len(args):
if args[i] == '--key':
h = args[i+1].lstrip('#')
key = (int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16))
i += 2
else:
dirs.append(args[i]); i += 1
if not dirs:
dirs = sorted(glob.glob('gfx*_extracted'))
total = 0
for d in dirs:
bmps = sorted(glob.glob(os.path.join(d, '*.bmp')))
for b in bmps:
try:
convert(b, key)
total += 1
except Exception as ex:
print(' ERROR %s: %s' % (b, ex))
print('%s -> %d png' % (d, len(bmps)))
print('TOTAL %d png (colorkey %02x%02x%02x)' % (total, *key))
if __name__ == '__main__':
main()