From 9c4fb6a6a4b0d63c5c165aefb95cbe5c0d9fdace Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 28 Jun 2015 23:50:50 -0700 Subject: Add a script to convert project-specific graphics. This is an alternative to terrible hacks like filename metadata, and might combat submodule bloat. --- gfx.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 gfx.py (limited to 'gfx.py') diff --git a/gfx.py b/gfx.py new file mode 100644 index 000000000..fd48da339 --- /dev/null +++ b/gfx.py @@ -0,0 +1,101 @@ +"""Supplementary scripts for graphics conversion.""" + +import os +import argparse + +from extras.pokemontools import gfx + + +# Graphics with inverted tilemaps that aren't covered by filepath_rules. +pics = [ + 'gfx/shrink1', + 'gfx/shrink2', +] + +def filepath_rules(filepath): + """Infer attributes of certain graphics by their location in the filesystem.""" + args = {} + + filedir, filename = os.path.split(filepath) + name, _, ext = filename.partition('.') # os.path.splitext only takes the last '.' + + if 'gfx/pics/' in filedir: + if name == 'front': + args['pal_file'] = os.path.join(filedir, 'normal.pal') + args['pic'] = True + args['animate'] = True + elif name == 'back': + args['pal_file'] = os.path.join(filedir, 'shiny.pal') + args['pic'] = True + + elif 'gfx/trainers' in filedir: + args['pic'] = True + + elif os.path.join(filedir, name) in pics: + args['pic'] = True + + if args.get('pal_file'): + args['palout'] = args['pal_file'] + + if args.get('pic'): + if ext == 'png': + w, h = gfx.png.Reader(filepath).asRGBA8()[:2] + w = min(w/8, h/8) + args['pic_dimensions'] = w, w + return args + + +def to_1bpp(filename, **kwargs): + filename, name, ext = gfx.try_decompress(filename) + if ext == '.1bpp': pass + elif ext == '.2bpp': gfx.export_2bpp_to_1bpp(filename, **kwargs) + elif ext == '.png': gfx.export_png_to_1bpp(filename, **kwargs) + +def to_2bpp(filename, **kwargs): + filename, name, ext = gfx.try_decompress(filename) + if ext == '.1bpp': gfx.export_1bpp_to_2bpp(filename, **kwargs) + elif ext == '.2bpp': pass + elif ext == '.png': gfx.export_png_to_2bpp(filename, **kwargs) + +def to_png(filename, **kwargs): + filename, name, ext = gfx.try_decompress(filename) + if ext == '.1bpp': gfx.export_1bpp_to_png(filename, **kwargs) + elif ext == '.2bpp': gfx.export_2bpp_to_png(filename, **kwargs) + elif ext == '.png': pass + +def compress(filename, **kwargs): + data = open(filename, 'rb').read() + lz_data = gfx.Compressed(data).output + open(filename + '.lz', 'wb').write(bytearray(lz_data)) + +def decompress(filename, **kwargs): + lz_data = open(filename, 'rb').read() + data = gfx.Decompressed(lz_data).output + name, ext = os.path.splitext(filename) + open(name, 'wb').write(bytearray(data)) + + +methods = { + '2bpp': to_2bpp, + '1bpp': to_1bpp, + 'png': to_png, + 'lz': compress, + 'unlz': decompress, +} + +def main(method_name, filenames=None): + if filenames is None: filenames = [] + for filename in filenames: + args = filepath_rules(filename) + method = methods.get(method_name) + if method: method(filename, **args) + +def get_args(): + ap = argparse.ArgumentParser() + ap.add_argument('method_name') + ap.add_argument('filenames', nargs='*') + args = ap.parse_args() + return args + +if __name__ == '__main__': + main(**get_args().__dict__) -- cgit v1.2.3 From 621a36ae1537dc72cb490a5c73aa5551e7bb55ea Mon Sep 17 00:00:00 2001 From: yenatch Date: Sat, 11 Jul 2015 10:34:26 -0700 Subject: Depend less on the submodule in gfx.py. --- gfx.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'gfx.py') diff --git a/gfx.py b/gfx.py index fd48da339..730c73451 100644 --- a/gfx.py +++ b/gfx.py @@ -3,7 +3,7 @@ import os import argparse -from extras.pokemontools import gfx +from extras.pokemontools import gfx, lz # Graphics with inverted tilemaps that aren't covered by filepath_rules. @@ -17,7 +17,7 @@ def filepath_rules(filepath): args = {} filedir, filename = os.path.split(filepath) - name, _, ext = filename.partition('.') # os.path.splitext only takes the last '.' + name, ext = os.path.splitext(filename) if 'gfx/pics/' in filedir: if name == 'front': @@ -38,7 +38,7 @@ def filepath_rules(filepath): args['palout'] = args['pal_file'] if args.get('pic'): - if ext == 'png': + if ext == '.png': w, h = gfx.png.Reader(filepath).asRGBA8()[:2] w = min(w/8, h/8) args['pic_dimensions'] = w, w @@ -46,31 +46,31 @@ def filepath_rules(filepath): def to_1bpp(filename, **kwargs): - filename, name, ext = gfx.try_decompress(filename) + _, ext = os.path.splitext(filename) if ext == '.1bpp': pass elif ext == '.2bpp': gfx.export_2bpp_to_1bpp(filename, **kwargs) elif ext == '.png': gfx.export_png_to_1bpp(filename, **kwargs) def to_2bpp(filename, **kwargs): - filename, name, ext = gfx.try_decompress(filename) + _, ext = os.path.splitext(filename) if ext == '.1bpp': gfx.export_1bpp_to_2bpp(filename, **kwargs) elif ext == '.2bpp': pass elif ext == '.png': gfx.export_png_to_2bpp(filename, **kwargs) def to_png(filename, **kwargs): - filename, name, ext = gfx.try_decompress(filename) + _, ext = os.path.splitext(filename) if ext == '.1bpp': gfx.export_1bpp_to_png(filename, **kwargs) elif ext == '.2bpp': gfx.export_2bpp_to_png(filename, **kwargs) elif ext == '.png': pass def compress(filename, **kwargs): data = open(filename, 'rb').read() - lz_data = gfx.Compressed(data).output + lz_data = lz.Compressed(data).output open(filename + '.lz', 'wb').write(bytearray(lz_data)) def decompress(filename, **kwargs): lz_data = open(filename, 'rb').read() - data = gfx.Decompressed(lz_data).output + data = lz.Decompressed(lz_data).output name, ext = os.path.splitext(filename) open(name, 'wb').write(bytearray(data)) @@ -88,7 +88,8 @@ def main(method_name, filenames=None): for filename in filenames: args = filepath_rules(filename) method = methods.get(method_name) - if method: method(filename, **args) + if method: + method(filename, **args) def get_args(): ap = argparse.ArgumentParser() -- cgit v1.2.3