Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

image.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # "THE BEER-WARE LICENSE" (Revision 42):
  4. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. # you can do whatever you want with this stuff. If we meet some day, and you
  6. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. # ----------------------------------------------------------------------------
  8. from PIL import Image
  9. import time
  10. import os
  11. class ImageScreen:
  12. def __init__(self, g, p, t = 0.2, i = 1, to = 20.0, bg = None):
  13. self.gui = g
  14. self.time = t
  15. self.iterations = i
  16. self.timeout = to
  17. self.background = bg
  18. scriptDir = os.path.dirname(os.path.realpath(__file__))
  19. self.path = os.path.join(scriptDir, "images", p)
  20. self.image = Image.open(self.path)
  21. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  22. self.restart()
  23. def restart(self):
  24. self.start = time.time()
  25. self.frame = time.time()
  26. self.count = 0
  27. self.done = 0
  28. self.image.seek(0)
  29. def finished(self):
  30. if self.done >= self.iterations:
  31. return True
  32. return (time.time() - self.start) >= self.timeout
  33. def draw(self):
  34. if self.image.is_animated:
  35. if (time.time() - self.frame) >= self.time:
  36. self.frame = time.time()
  37. self.count = (self.count + 1) % self.image.n_frames
  38. if self.count == 0:
  39. self.done += 1
  40. self.image.seek(self.count)
  41. p = self.image.getpalette()
  42. for x in range(0, self.image.width):
  43. for y in range(0, self.image.height):
  44. v = self.image.getpixel((x, y))
  45. if isinstance(v, int):
  46. c = None
  47. if self.background != None:
  48. if "transparency" in self.image.info:
  49. if v == self.image.info["transparency"]:
  50. c = self.background
  51. else:
  52. if v == self.image.info["background"]:
  53. c = self.background
  54. if c == None:
  55. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  56. self.gui.set_pixel(x, y, c)
  57. else:
  58. self.gui.set_pixel(x, y, v)
  59. if __name__ == "__main__":
  60. import util
  61. t = util.getTarget()
  62. from manager import Manager
  63. m = Manager(t)
  64. scriptDir = os.path.dirname(os.path.realpath(__file__))
  65. imageDir = os.path.join(scriptDir, "images")
  66. for f in os.listdir(os.fsencode(imageDir)):
  67. filename = os.fsdecode(f)
  68. m.add(ImageScreen(t, os.path.join(imageDir, filename)))
  69. m.restart()
  70. t.debug_loop(m.draw)