Files
Bubberstation/tools/HitboxExpander/__main__.py
Tad Hardesty 67b4aa190e Use portable Python for map merge hooks, other tools (#55658)
* Add tools/bootstrap/python

* Add dependencies.sh entry for Python

* Use bootstrapped Python for HitboxExpander

* Use bootstrapped Python when installing hooks

Also:
- Adds tools/hooks/Uninstall.bat
- Removes the need to re-run Install.bat when a .hook file changes

Python path temporarily includes tools/mapmerge2 as before:
- PYTHONPATH in tools/hooks/python.sh handles Linux/virtualenv
- python36._pth handles Windows portable

* Move DMI merge driver to its own directory

* Use bootstrapped Python for makeChangelog.bat

* Set window title in bootstrap PS scripts

* Use slashes in hook paths

* Use relative imports in mapmerge2

* Use bootstrapped Python for mapmerge

* Update mapmerge2 readme

* Skip non-3way conflicts

* Fix relative path issue in posthoc merger

* Add missing call and %* to .bat files

* When installing hooks, don't uninstall them all first

* Make dependencies.sh POSIX

* Move update_paths.py to its own folder

* Use line buffering for Python stdout/stderr

It was switching to fully-buffered mode due to being piped, meaning we
wouldn't see output until the script ended. This is undesirable.

* Use new path to dmi.test in CI suite

* Validate system Python version in bootstrap script

* Add install advice to bootstrap script

* Update path to requirements.txt in CI

* Add upgrader for existing installations
2020-12-22 22:22:15 +01:00

101 lines
2.4 KiB
Python
Executable File

import os
import sys
import inspect
import shutil
import PIL.Image as Image
current_dir = os.path.split(inspect.getfile(inspect.currentframe()))[0]
def PngSave(im, file):
# From http://blog.client9.com/2007/08/28/python-pil-and-png-metadata-take-2.html
# these can be automatically added to Image.info dict
# they are not user-added metadata
reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect')
# undocumented class
import PIL.PngImagePlugin as PngImagePlugin
meta = PngImagePlugin.PngInfo()
# copy metadata into new object
for k,v in im.info.items():
if k in reserved: continue
meta.add_text(k, str(v), 0)
# and save
im.save(file, "PNG", pnginfo=meta)
def ProcessFile(path):
name, ext = os.path.splitext(path)
ext = ext.lower()
if (ext != ".dmi" and ext != ".png") or os.path.splitext(name)[1] == ".new":
return
try:
im = Image.open(path)
print(name + ": " + im.format, im.size, im.mode)
if im.mode != "RGBA":
return
width, height = im.size
pix = im.load()
n_transparent = 0
make_opaque = []
def add(x, y):
if pix[x, y][3] == 0:
make_opaque.append((x, y))
for x in range(0, width):
for y in range(0, height):
if pix[x, y][3] > 0:
if x > 0:
add(x - 1, y)
if x < width - 1:
add(x + 1, y)
if y > 0:
add(x, y - 1)
if y < height - 1:
add(x, y + 1)
else:
n_transparent += 1
for coords in make_opaque:
pix[coords] = (0, 0, 0, 1)
PngSave(im, path)
except Exception as e:
print("Could not process " + name)
print(e)
root_dir = os.path.abspath(os.path.join(current_dir, "../../"))
icons_dir = os.path.join(root_dir, "icons")
def Main():
if len(sys.argv) != 2:
if os.name == 'nt':
print("Usage: drag-and-drop a .dmi onto `Hitbox Expander.bat`\n or")
with open(os.path.join(current_dir, "README.txt")) as f:
print(f.read())
return 0
try:
with open(sys.argv[1]):
ProcessFile(os.path.abspath(sys.argv[1]))
return 0
except IOError:
pass
for root, subdirs, files in os.walk(icons_dir):
for file in files:
if file == sys.argv[1]:
path = os.path.join(root, file)
ProcessFile(path)
return 0
print("File not found: " + sys.argv[1])
if __name__ == "__main__":
Main()