ani2spritesheet.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def analyzeANIFile(filePath):
  6. with open(filePath,'rb') as f:
  7. if f.read(4) != b'RIFF':
  8. return {"code":-1,"msg":"File is not a ANI File!"}
  9. logging.debug('文件头检查完成!')
  10. fileSize = int.from_bytes(f.read(4), byteorder='little', signed=False)
  11. # if os.path.getsize(filePath) != fileSize:
  12. # return {"code":-2,"msg":"File is damaged!"}
  13. logging.debug('文件长度检查完成!')
  14. if f.read(4) != b'ACON':
  15. return {"code":-1,"msg":"File is not a ANI File!"}
  16. logging.debug('魔数检查完成!')
  17. frameRate = (1/60)*1000
  18. while(True):
  19. chunkName = f.read(4)
  20. if chunkName == b'LIST':
  21. break
  22. chunkSize = int.from_bytes(f.read(4), byteorder='little', signed=False)
  23. if chunkName.lower() == b'rate':
  24. logging.debug('发现自定义速率!')
  25. frameRate = frameRate * int.from_bytes(f.read(4), byteorder='little', signed=False)
  26. logging.warning('发现自定义速率!由于GIF限制,将取第一帧与第二帧的速率作为整体速率!')
  27. f.read(chunkSize - 4)
  28. else:
  29. logging.debug('发现自定义Chunk!')
  30. f.read(chunkSize)
  31. listChunkSize = int.from_bytes(f.read(4), byteorder='little', signed=False)
  32. if f.read(4) != b'fram':
  33. return {"code":-3,"msg":"File not a ANI File!(No Frames)"}
  34. logging.debug('frame头检查完成!')
  35. frameList = []
  36. nowSize = 4
  37. while(nowSize < listChunkSize):
  38. if f.read(4) != b'icon':
  39. return {"code":-4,"msg":"File not a ANI File!(Other Kind Frames)"}
  40. nowSize += 4
  41. subChunkSize = int.from_bytes(f.read(4), byteorder='little', signed=False)
  42. nowSize += 4
  43. frameList.append(f.read(subChunkSize))
  44. nowSize += subChunkSize
  45. return {"code":0,"msg":frameList,"frameRate":frameRate}
  46. if __name__ == '__main__':
  47. OUTPUT_SIZE = (48,48)
  48. if len(sys.argv) < 2:
  49. logging.fatal("Usage:python ani2spritesheet.py <inputFile> <outputFile,Option>")
  50. else:
  51. res = analyzeANIFile(sys.argv[1])
  52. GIFframes = []
  53. if res["code"] == 0:
  54. logging.info('ANI文件分析完成,帧提取完成!')
  55. output = Image.new("RGBA", (OUTPUT_SIZE[0], OUTPUT_SIZE[1] * len(res["msg"])))
  56. for frameIndex in range(len(res["msg"])):
  57. frameImage = Image.open(io.BytesIO(res["msg"][frameIndex]),formats=['cur']).convert('RGBA')
  58. extracted_frame = frameImage.resize(OUTPUT_SIZE)
  59. position = (0, OUTPUT_SIZE[0] * frameIndex)
  60. output.paste(extracted_frame, position)
  61. if(len(sys.argv) >= 3):
  62. output.save(sys.argv[2],format="PNG")
  63. else:
  64. output.save(f"{sys.argv[1].strip('.ani')}.png",format="PNG")
  65. logging.info('SpriteSheet生成完成!')
  66. else:
  67. logging.fatal(res["msg"])