From 6f1ea202427d3db8b4b2395fc80349e491615800 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 30 May 2026 20:14:46 +0000 Subject: [PATCH] Init commit --- extract_egf.py | 155 +++++++++++++++++++++++++++++++++++++++++++++++++ to_png.py | 57 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 extract_egf.py create mode 100644 to_png.py diff --git a/extract_egf.py b/extract_egf.py new file mode 100644 index 0000000..163a4df --- /dev/null +++ b/extract_egf.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python3 +"""Extract bitmap images from .egf files (Win32 PE resource containers). + +EGF files are PE binaries whose images Resource Hacker shows as RT_BITMAP +(type 2) resources, stored as raw DIBs (BITMAPINFOHEADER, no 14-byte BM +file header). This walks the PE resource directory, pulls every bitmap, and +writes a proper .bmp by prepending a reconstructed BITMAPFILEHEADER. + +Usage: + python3 extract_egf.py [file_or_dir ...] +No args -> all *.egf in current directory. +""" +import struct, sys, os, glob + +RT_BITMAP = 2 + +def u16(d, o): return struct.unpack_from(' %d images (%s)' % (f, n, outdir)) + total += n + print('TOTAL %d images' % total) + +if __name__ == '__main__': + main() diff --git a/to_png.py b/to_png.py new file mode 100644 index 0000000..83f15ea --- /dev/null +++ b/to_png.py @@ -0,0 +1,57 @@ +#!/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()