gif2spritesheet.py 925 B

1234567891011121314151617181920212223
  1. from PIL import Image
  2. import io,os,sys
  3. import logging
  4. logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',level=logging.INFO)
  5. if __name__ == '__main__':
  6. if len(sys.argv) < 2:
  7. logging.fatal("Usage:python gif2spritesheet.py <inputFile> <outputFile,Option>")
  8. else:
  9. OUTPUT_SIZE = (48,48)
  10. gif = Image.open(f"{sys.argv[1]}")
  11. output = Image.new("RGBA", (OUTPUT_SIZE[0], OUTPUT_SIZE[1] * gif.n_frames))
  12. for frame in range(0,gif.n_frames):
  13. gif.seek(frame)
  14. extracted_frame = gif.resize(OUTPUT_SIZE)
  15. position = (0, OUTPUT_SIZE[0] * frame)
  16. output.paste(extracted_frame, position)
  17. if(len(sys.argv) >= 3):
  18. output.save(sys.argv[2],format="PNG")
  19. else:
  20. output.save(f"{sys.argv[1].strip('.gif')}.png",format="PNG")
  21. logging.info("转换完成!")