Guest User

transfer_png_sprite_offsets.py

a guest
Dec 14th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import sys, os, png
  2.  
  3. usage = "Transfers ZDoom/SLADE-format grAb chunk from donor to target PNGs.\n"\
  4. "Usage:\n\tpython.exe transfer_png_sprite_offsets.py donor target\n\n"\
  5. "Donor and target can either both be filenames or directories.\n"\
  6. "Subdirectories are not included."
  7.  
  8. def do_the_thing(dnr_path, tgt_path):
  9. dnr_png = png.Reader(filename=dnr_path)
  10. tgt_png = png.Reader(filename=tgt_path)
  11. dnr_chunks = list(dnr_png.chunks())
  12. tgt_chunks = list(tgt_png.chunks())
  13. if dnr_chunks[1][0] == b'grAb':
  14. if tgt_chunks[1][0] != b'grAb':
  15. tgt_chunks.insert(1,dnr_chunks[1])
  16. with open(tgt_path, 'wb') as output:
  17. png.write_chunks(output, tgt_chunks)
  18. else:
  19. print("\nTarget already has offsets: " + name)
  20. else:
  21. input("\nNo donor offset chunk found: " + name)
  22.  
  23. if len(sys.argv) != 3:
  24. input(usage)
  25. sys.exit()
  26. donor, target = sys.argv[1:3]
  27.  
  28. if os.path.isfile(donor) and os.path.isfile(target):
  29. do_the_thing(donor, target)
  30. elif os.path.isdir(donor) and os.path.isdir(target):
  31. namelist = [f for (_,_,fnames) in os.walk(target) for f in fnames]
  32. for name in namelist:
  33. donor_path = os.path.join(donor, name)
  34. target_path = os.path.join(target, name)
  35. if os.path.isfile(donor_path):
  36. do_the_thing(donor_path, target_path)
  37. else:
  38. print("\nCould not find a matching donor: " + name)
  39. else:
  40. input(usage)
  41. sys.exit()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment