Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # builtin imports
  2. import argparse
  3. from pathlib import Path
  4. from math import ceil
  5.  
  6. # 3d Party imports
  7. import deftree
  8. from PIL import Image
  9.  
  10.  
  11. def get_all_images(atlas_root):
  12. return [image.get_attribute("image").value for image in atlas_root.elements("images")]
  13.  
  14.  
  15. def scale_image(image, percent):
  16. img = Image.open(image)
  17. new_width = ceil(img.width * (percent/100))
  18. width_percent = (new_width/float(img.size[0]))
  19. new_height = int((float(img.size[1])*float(width_percent)))
  20. img = img.resize((new_width, new_height), Image.ANTIALIAS)
  21. img.save(image)
  22.  
  23.  
  24. def main(argument_style):
  25. args = argument_style()
  26. root_path = args.root
  27. atlas_path = args.atlas
  28. percent = args.percent
  29. if not root_path or not atlas_path or not percent:
  30. print_help()
  31. return
  32. tree = deftree.parse(atlas_path)
  33. root = tree.get_root()
  34. for image in get_all_images(root):
  35. path = Path(root_path) / image[1:]
  36. scale_image(path, percent)
  37.  
  38.  
  39. def _parser():
  40. parser = argparse.ArgumentParser(description='Commandline tool to scale atlases')
  41. parser.add_argument('-r', '--root', dest='root', )
  42. parser.add_argument('-a', '--atlas', dest='atlas')
  43. parser.add_argument('-p', '--percent', dest='percent', type=int)
  44. return parser
  45.  
  46.  
  47. def arguments():
  48. parser = _parser()
  49. input_args = parser.parse_args()
  50. return input_args
  51.  
  52.  
  53. def mock_up():
  54. parser = _parser()
  55. input_args = parser.parse_args(['--root', 'test/repo', '-a', 'test/repo/assets/atlases/avatars.atlas', "-p", "90"])
  56. return input_args
  57.  
  58.  
  59. def print_help():
  60. print("Usage: python resize_atlas.py -r 'project/root/' -a 'project/root/game.atlas' -p 10\n")
  61. print("The arguments are:")
  62. print(" --root (-r) Root the the project")
  63. print(" --atlas (-a) Atlas that we should resize the images of")
  64. print(" --percent (-p) The new size in percent (90 will resave the image at 90%)")
  65.  
  66.  
  67. if __name__ == '__main__':
  68. main(arguments)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement