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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # Uses the pillow Python Imaging Library:
  3. # https://github.com/python-pillow/Pillow
  4. #
  5. # ----------------------------------------------------------------------------
  6. # "THE BEER-WARE LICENSE" (Revision 42):
  7. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  8. # you can do whatever you want with this stuff. If we meet some day, and you
  9. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  10. # ----------------------------------------------------------------------------
  11. from PIL import Image
  12. import time
  13. import os
  14. class ImageScreen:
  15. def __init__(self, g, p, t = 0.2, i = 1, to = 10.0, bg = None):
  16. self.gui = g
  17. self.time = t
  18. self.iterations = i
  19. self.timeout = to
  20. self.background = bg
  21. scriptDir = os.path.dirname(os.path.realpath(__file__))
  22. self.path = os.path.join(scriptDir, "images", p)
  23. self.image = Image.open(self.path)
  24. # for some reason non-animated images don't even have this attribute
  25. if not hasattr(self.image, "is_animated"):
  26. self.image.is_animated = False
  27. self.image.n_frames = 1
  28. # automatically crop and scale large images
  29. if not self.image.is_animated and ((self.image.width > self.gui.width) or (self.image.height > self.gui.height)):
  30. self.image = self.image.crop(self.image.getbbox())
  31. self.image = self.image.resize((self.gui.width, self.gui.height),
  32. Image.Resampling.NEAREST)
  33. # new image object is also missing these
  34. self.image.is_animated = False
  35. self.image.n_frames = 1
  36. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  37. self.xOff = int((self.gui.width - self.image.width) / 2)
  38. self.yOff = int((self.gui.height - self.image.height) / 2)
  39. self.restart()
  40. def restart(self):
  41. self.start = time.time()
  42. self.frame = time.time()
  43. self.count = 0
  44. self.done = 0
  45. self.image.seek(0)
  46. def finished(self):
  47. if self.done >= self.iterations:
  48. return True
  49. return (time.time() - self.start) >= self.timeout
  50. def draw(self):
  51. if self.image.is_animated:
  52. if (time.time() - self.frame) >= self.time:
  53. self.frame = time.time()
  54. self.count = (self.count + 1) % self.image.n_frames
  55. if self.count == 0:
  56. self.done += 1
  57. self.image.seek(self.count)
  58. p = self.image.getpalette()
  59. for x in range(0, self.image.width):
  60. for y in range(0, self.image.height):
  61. v = self.image.getpixel((x, y))
  62. if isinstance(v, int):
  63. c = None
  64. if self.background != None:
  65. if "transparency" in self.image.info:
  66. if v == self.image.info["transparency"]:
  67. c = self.background
  68. else:
  69. if v == self.image.info["background"]:
  70. c = self.background
  71. if c == None:
  72. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  73. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  74. else:
  75. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  76. if __name__ == "__main__":
  77. import util
  78. t = util.getTarget()
  79. from manager import Manager
  80. m = Manager(t)
  81. scriptDir = os.path.dirname(os.path.realpath(__file__))
  82. imageDir = os.path.join(scriptDir, "images")
  83. for f in os.listdir(os.fsencode(imageDir)):
  84. filename = os.fsdecode(f)
  85. m.add(ImageScreen(t, filename))
  86. m.restart()
  87. t.debug_loop(m.draw)