Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Created March 14, 2019 10:23
Show Gist options
  • Save Jerakin/cf0e1c4ba8eed98a2f9ffd2797a8e837 to your computer and use it in GitHub Desktop.
Save Jerakin/cf0e1c4ba8eed98a2f9ffd2797a8e837 to your computer and use it in GitHub Desktop.
# builtin imports
import argparse
from pathlib import Path
from math import ceil
# 3d Party imports
import deftree
from PIL import Image
def get_all_images(atlas_root):
return [image.get_attribute("image").value for image in atlas_root.elements("images")]
def scale_image(image, percent):
img = Image.open(image)
new_width = ceil(img.width * (percent/100))
width_percent = (new_width/float(img.size[0]))
new_height = int((float(img.size[1])*float(width_percent)))
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img.save(image)
def main(argument_style):
args = argument_style()
root_path = args.root
atlas_path = args.atlas
percent = args.percent
if not root_path or not atlas_path or not percent:
print_help()
return
tree = deftree.parse(atlas_path)
root = tree.get_root()
for image in get_all_images(root):
path = Path(root_path) / image[1:]
scale_image(path, percent)
def _parser():
parser = argparse.ArgumentParser(description='Commandline tool to scale atlases')
parser.add_argument('-r', '--root', dest='root', )
parser.add_argument('-a', '--atlas', dest='atlas')
parser.add_argument('-p', '--percent', dest='percent', type=int)
return parser
def arguments():
parser = _parser()
input_args = parser.parse_args()
return input_args
def mock_up():
parser = _parser()
input_args = parser.parse_args(['--root', 'test/repo', '-a', 'test/repo/assets/atlases/avatars.atlas', "-p", "90"])
return input_args
def print_help():
print("Usage: python resize_atlas.py -r 'project/root/' -a 'project/root/game.atlas' -p 10\n")
print("The arguments are:")
print(" --root (-r) Root the the project")
print(" --atlas (-a) Atlas that we should resize the images of")
print(" --percent (-p) The new size in percent (90 will resave the image at 90%)")
if __name__ == '__main__':
main(arguments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment