ani2gif.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if len(sys.argv) < 2:
  48. logging.fatal("Usage:python ani2gif.py <inputFile> <outputFile,Option>")
  49. else:
  50. res = analyzeANIFile(sys.argv[1])
  51. GIFframes = []
  52. if res["code"] == 0:
  53. logging.info('ANI文件分析完成,帧提取完成!')
  54. for frame in res["msg"]:
  55. frameImage = Image.open(io.BytesIO(frame),formats=['CUR']).convert('RGBA')
  56. GIFframes.append(frameImage)
  57. if(len(sys.argv) >= 3):
  58. GIFframes[0].save(sys.argv[2],format="GIF",save_all=True, append_images=GIFframes[1:], optimize=False, duration=res["frameRate"], loop=0, transparency=0, disposal=2)
  59. else:
  60. GIFframes[0].save(f"{sys.argv[1].strip('.ani')}.gif",format="GIF",save_all=True, append_images=GIFframes[1:], optimize=False, duration=res["frameRate"], loop=0, transparency=0, disposal=2)
  61. logging.info('GIF生成完成!')
  62. else:
  63. logging.fatal(res["msg"])