Advertisement
Guest User

Untitled

a guest
Jan 5th, 2023
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | Source Code | 0 0
  1. import glob
  2. from PIL import Image
  3.  
  4. #Find all gifs which have an underscore in the middle and save the prefix names
  5. prefixes = set()
  6. for filename in glob.glob('*.gif'):
  7.     name, _ = filename.split('.')
  8.     if '_' in name:
  9.         prefix, _ = name.split('_')
  10.         prefixes.add(prefix)
  11.  
  12.  
  13. #The first element of the tuple is the position suffix
  14. #The second one is another tuple containing the column
  15. #and the row to which each position be pasted.
  16. #Example: fr1 (front 1) will be pasted into first row, first column
  17. #lf2 (left 2) will be pasted to column 1 row 3 (the fourth row)
  18. positions = [("fr1", (0, 0)),
  19.     ("fr2", (1, 0)) ,
  20.     ("rt1", (0, 1)),
  21.     ("rt2", (1, 1)),
  22.     ("bk1", (0, 2)),
  23.     ("bk2", (1, 2)),
  24.     ("lf1", (0, 3)),
  25.     ("lf2", (1, 3))]
  26.  
  27. #For each prefix, create a new image with alpha channel
  28. for prefix in prefixes:
  29.     sprite = Image.new('RGBA', (64, 128))
  30.     for position in positions:
  31.         suffix, coord = position
  32.         src_img = prefix + "_" + suffix + ".gif"
  33.         img = Image.open(src_img)
  34.         sprite.paste(img, (coord[0] * 32, coord[1] * 32))
  35.  
  36.         #Change alpha value of white pixels to 0
  37.         #Snippet found at:
  38.         #https://stackoverflow.com/questions/765736/how-to-use-pil-to-make-all-white-pixels-transparent
  39.         pixdata = sprite.load()
  40.         width, height = sprite.size
  41.         for y in range(height):
  42.             for x in range(width):
  43.                 if pixdata[x, y] == (255, 255, 255, 255):
  44.                     pixdata[x, y] = (255, 255, 255, 0)
  45.     #save sprite
  46.     sprite.save(prefix + ".png", "PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement