Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import util
  15. class ImageScreen:
  16. def __init__(self, g, p, t = 0.2, i = 1, to = 10.0, bg = None):
  17. self.gui = g
  18. self.time = t
  19. self.iterations = i
  20. self.timeout = to
  21. self.background = bg
  22. scriptDir = os.path.dirname(os.path.realpath(__file__))
  23. self.path = os.path.join(scriptDir, "images", p)
  24. self.image = Image.open(self.path)
  25. # for some reason non-animated images don't even have this attribute
  26. if not hasattr(self.image, "is_animated"):
  27. self.image.is_animated = False
  28. self.image.n_frames = 1
  29. # automatically crop and scale large images
  30. if not self.image.is_animated and ((self.image.width > self.gui.width) or (self.image.height > self.gui.height)):
  31. # crop to visible area
  32. self.image = self.image.crop(self.image.getbbox())
  33. # keep the aspect ratio and fit within visible area
  34. ratio = self.image.width / self.image.height
  35. width = self.gui.width
  36. height = self.gui.height
  37. if width < height:
  38. width = self.gui.width
  39. height = int(width / ratio)
  40. else:
  41. height = self.gui.height
  42. width = int(ratio * height)
  43. # resize
  44. self.image = self.image.resize((width, height),
  45. Image.Resampling.NEAREST)
  46. # new image object is also missing these
  47. self.image.is_animated = False
  48. self.image.n_frames = 1
  49. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  50. self.xOff = int((self.gui.width - self.image.width) / 2)
  51. self.yOff = int((self.gui.height - self.image.height) / 2)
  52. self.restart()
  53. def restart(self):
  54. self.start = time.time()
  55. self.frame = time.time()
  56. self.count = 0
  57. self.done = 0
  58. self.image.seek(0)
  59. def finished(self):
  60. if self.done >= self.iterations:
  61. return True
  62. return (time.time() - self.start) >= self.timeout
  63. def draw(self):
  64. if self.image.is_animated:
  65. now = time.time()
  66. if (now - self.frame) >= self.time:
  67. self.frame = now
  68. self.count = (self.count + 1) % self.image.n_frames
  69. if self.count == 0:
  70. self.done += 1
  71. self.image.seek(self.count)
  72. p = self.image.getpalette()
  73. for x in range(0, self.image.width):
  74. for y in range(0, self.image.height):
  75. v = self.image.getpixel((x, y))
  76. if isinstance(v, int):
  77. c = None
  78. if self.background != None:
  79. if "transparency" in self.image.info:
  80. if v == self.image.info["transparency"]:
  81. c = self.background
  82. else:
  83. if v == self.image.info["background"]:
  84. c = self.background
  85. if c == None:
  86. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  87. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  88. else:
  89. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  90. if __name__ == "__main__":
  91. import util
  92. t = util.getTarget()
  93. from manager import Manager
  94. m = Manager(t)
  95. scriptDir = os.path.dirname(os.path.realpath(__file__))
  96. imageDir = os.path.join(scriptDir, "images")
  97. for f in os.listdir(os.fsencode(imageDir)):
  98. filename = os.fsdecode(f)
  99. d = ImageScreen(t, filename)
  100. m.add(d)
  101. if filename != "Favicon.png":
  102. continue
  103. # dump generated image to console, for embedding in Pico script
  104. print()
  105. print("Dumping image to img_tmp.py")
  106. with open("img_tmp.py", "w") as f:
  107. f.write("# Image \"" + filename + "\"" + os.linesep)
  108. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  109. f.write("img_data = [" + os.linesep)
  110. for y in range(0, d.image.height):
  111. f.write(" [" + os.linesep)
  112. for x in range(0, d.image.width):
  113. s = str(d.image.getpixel((x, y)))
  114. f.write(" " + s + "," + os.linesep)
  115. f.write(" ]," + os.linesep)
  116. f.write("]" + os.linesep)
  117. print()
  118. m.restart()
  119. t.loop(m.draw)