Last active
July 31, 2016 13:56
-
-
Save PJB3005/1f8911552b251672ad582f4f3a92af80 to your computer and use it in GitHub Desktop.
32->64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# Requires Pillow (fork of PIL) and BYONDTools. | |
# For Pillow you can just run "$ pip instal pillow" (without quotes) in a terminal. | |
# If your PATH doesn't include Python just go to %Python directory%/scripts/, it's in there. | |
# As for BYONDTools: download a ZIP of https://gitlab.com/N3X15/ByondTools, then run setup.py install from a terminal. | |
from __future__ import print_function | |
from byond.DMI import DMI | |
from byond.DMI.State import State | |
import PIL.Image | |
import os | |
import os.path | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("dmifile", help="The DMIs to scale.", nargs="*", type=str) | |
parser.add_argument("-r", "--recurse", help="In conjunction with taking in DMI files, makes the script also recurse through the provided directory to find DMI files.") | |
args = parser.parse_args() | |
if args.recurse: | |
for dirpath, dirnames, filenames in os.walk(args.recurse): | |
for filename in filenames: | |
if (filename[-3:] != "dmi"): | |
continue | |
args.dmifile.append(os.path.join(dirpath, filename)) | |
if len(args.dmifile) < 1: | |
print("Unable to find any mapfiles to upscale.") | |
exit() | |
for fullpath in args.dmifile: | |
try: | |
print("Upscaling %s" % (fullpath)) | |
upscale(fullpath) | |
except Exception as e: | |
print("Error upscaling %s: %s" % (fullpath, e)) | |
def upscale(dmifile): | |
dmi = DMI(dmifile) | |
dmi.loadAll() | |
upscaled = DMI(dmifile) | |
upscaled.icon_height = dmi.icon_height * 2 | |
upscaled.icon_width = dmi.icon_width * 2 | |
for state in dmi.states.values(): | |
new_state = State(state.name) | |
new_state.frames = state.frames | |
new_state.dirs = state.dirs | |
new_state.movement = state.movement | |
new_state.loop = state.loop | |
new_state.rewind = state.rewind | |
new_state.delay = state.delay | |
for icon in state.icons: | |
new_icon = icon.resize((upscaled.icon_width, upscaled.icon_height), PIL.Image.NEAREST) | |
new_state.icons.append(new_icon) | |
upscaled.states[new_state.name] = new_state | |
upscaled.save(dmifile) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function, division | |
import re | |
import argparse | |
import os | |
import os.path | |
upscale_re = re.compile("pixel_(x|y|z|w) = (-?\d+)") | |
args = None | |
def main(): | |
global args | |
parser = argparse.ArgumentParser(prog="Upscale_map.py", description="Multiplies (or divides) all pixel offsets in a BYOND DMM map file by a specified factor.", version="0.1") | |
parser.add_argument("mapfile", help="The DMM mapfile to scale.", nargs="*", type=argparse.FileType('r+')) | |
parser.add_argument("-r", "--recurse", help="In conjunction with taking in map files, makes the script also recurse through the provided directory to find DMM files.") | |
parser.add_argument("--factor", nargs="?", default=2, help="The factor with which to multiply the pixel_x or pixel_y values in the map file.", type=int) | |
parser.add_argument("-d", "--divide", action="store_true", help="Divide the pixel offsets instead of multiplying them.") | |
args = parser.parse_args() | |
# print(args) | |
for dirpath, dirnames, filenames in os.walk(args.recurse): | |
for filename in filenames: | |
if (filename[-3:] != "dmm"): | |
continue | |
fullpath = os.path.join(dirpath, filename) | |
args.mapfile.append(open(fullpath, "r+")) | |
if len(args.mapfile) < 1: | |
print("Unable to find any mapfiles to upscale.") | |
exi() | |
for mapfile in args.mapfile: | |
upscaled, count = upscale_re.subn(replace, mapfile.read()) | |
mapfile.seek(0) | |
mapfile.truncate() | |
mapfile.write(upscaled) | |
print("Updated %s pixel offsets." % (count)) | |
mapfile.close() | |
def replace(match): | |
#print(match.group(1), match.group(2)) | |
offset_type = match.group(1) | |
amount = 0 | |
try: | |
if args.divide: | |
amount = int(match.group(2)) // args.factor | |
else: | |
amount = int(match.group(2)) * args.factor | |
except: | |
print("Error attempting to read pixel_%s offset '%s': can not convert to integer." % (offset_type, match.group(2))) | |
return "pixel_%s = %s" % (offset_type, match.group(2)) | |
# print(offset_type, amount) | |
return "pixel_%s = %s" % (offset_type, amount) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment