Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

image.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.xOff = int((self.gui.width - self.image.width) / 2)
  23. self.yOff = int((self.gui.height - self.image.height) / 2)
  24. self.restart()
  25. def restart(self):
  26. self.start = time.time()
  27. self.frame = time.time()
  28. self.count = 0
  29. self.done = 0
  30. self.image.seek(0)
  31. def finished(self):
  32. if self.done >= self.iterations:
  33. return True
  34. return (time.time() - self.start) >= self.timeout
  35. def draw(self):
  36. if self.image.is_animated:
  37. if (time.time() - self.frame) >= self.time:
  38. self.frame = time.time()
  39. self.count = (self.count + 1) % self.image.n_frames
  40. if self.count == 0:
  41. self.done += 1
  42. self.image.seek(self.count)
  43. p = self.image.getpalette()
  44. for x in range(0, self.image.width):
  45. for y in range(0, self.image.height):
  46. v = self.image.getpixel((x, y))
  47. if isinstance(v, int):
  48. c = None
  49. if self.background != None:
  50. if "transparency" in self.image.info:
  51. if v == self.image.info["transparency"]:
  52. c = self.background
  53. else:
  54. if v == self.image.info["background"]:
  55. c = self.background
  56. if c == None:
  57. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  58. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  59. else:
  60. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  61. if __name__ == "__main__":
  62. import util
  63. t = util.getTarget()
  64. from manager import Manager
  65. m = Manager(t)
  66. scriptDir = os.path.dirname(os.path.realpath(__file__))
  67. imageDir = os.path.join(scriptDir, "images")
  68. for f in os.listdir(os.fsencode(imageDir)):
  69. filename = os.fsdecode(f)
  70. m.add(ImageScreen(t, filename))
  71. m.restart()
  72. t.debug_loop(m.draw)