slideshow.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. import os
  3. import struct
  4. from flipper.app import App
  5. from flipper.assets.icon import file2image
  6. class Main(App):
  7. MAGIC = 0x72676468
  8. VERSION = 1
  9. def init(self):
  10. self.parser.add_argument("-i", "--input", help="input folder", required=True)
  11. self.parser.add_argument("-o", "--output", help="output file", required=True)
  12. self.parser.set_defaults(func=self.pack)
  13. def pack(self):
  14. if not os.path.exists(self.args.input):
  15. self.logger.error(f'"{self.args.input}" does not exist')
  16. return 1
  17. file_idx = 0
  18. images = []
  19. while True:
  20. frame_filename = os.path.join(self.args.input, f"frame_{file_idx:02}.png")
  21. if not os.path.exists(frame_filename):
  22. break
  23. self.logger.debug(f"working on {frame_filename}")
  24. try:
  25. images.append(file2image(frame_filename))
  26. self.logger.debug(f"Processed frame #{file_idx}")
  27. file_idx += 1
  28. except Exception as e:
  29. self.logger.error(e)
  30. return 3
  31. widths = set(img.width for img in images)
  32. heights = set(img.height for img in images)
  33. if len(widths) != 1 or len(heights) != 1:
  34. self.logger.error("All images must have same dimensions!")
  35. return 2
  36. data = struct.pack(
  37. "<IBBBB", self.MAGIC, self.VERSION, widths.pop(), heights.pop(), len(images)
  38. )
  39. for image in images:
  40. data += struct.pack("<H", len(image.data))
  41. data += image.data
  42. with open(self.args.output, mode="wb") as file:
  43. file.write(data)
  44. return 0
  45. if __name__ == "__main__":
  46. Main()()