Advertisement
vlatkovski

NecroDancer Sprite Shadow Remover

Apr 26th, 2020
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # CALL THE FUNCTION search ON THE DIRECTORY OF YOUR MOD
  2. # THE MOD SHOULD HAVE THE data FOLDER WITH ALL IMAGES IN IT
  3. # IT WILL FIX ALL SPRITES SO THEY HAVE NO SHADOWS
  4. # MIGHT BE CONSIDERED CHEATING
  5.  
  6.  
  7. import os
  8. from PIL import Image
  9.  
  10.  
  11. def fixSprite(image, filepath):
  12.     image = image.convert("RGBA")
  13.     width, height = image.size
  14.     pixels = image.load()
  15.     total = 0 # non-transparent pixels
  16.     black = 0 # valid cells in the second part of the image (the shadows)
  17.     offset = height//2
  18.     for x in range(width):
  19.         for y in range(height):
  20.             r, g, b, a = pixels[x, y]
  21.             if a != 255:
  22.                 continue
  23.             total += 1
  24.             if y < offset or r != 0 or g != 0 or b != 0:
  25.                 continue
  26.             _, _, _, a2 = pixels[x, y - offset]
  27.             if a2 != 255:
  28.                 continue
  29.             black += 1
  30.     if total < 0.1*width*height or not (0.40*total <= black and black <= 0.55*total):
  31.         # Not a valid sprite, some other image
  32.         return False
  33.     for x in range(width):
  34.         for y in range(offset, height):
  35.             pixels[x, y] = pixels[x, y - offset]
  36.     image.save(filepath)
  37.     # The sprite was (presumably) modified successfully
  38.     return True
  39.  
  40.    
  41. def search(currdir):
  42.     for filename in os.listdir(currdir):
  43.         filepath = os.path.join(currdir, filename)
  44.         if os.path.isdir(filepath):
  45.             search(filepath)
  46.         elif filename.endswith(".png"):
  47.             with Image.open(filepath) as image:
  48.                 if fixSprite(image, filepath):
  49.                     print(filename, "is good!")
  50.  
  51.  
  52. def main():
  53.     directory = "D:\\Vlatko\\Projects\\NecrodancerSpriteDuplicator\\VlatkoMod"
  54.     search(directory)
  55.  
  56.  
  57. if __name__ == "__main__":
  58.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement