Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

image.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. self.image = self.image.crop(self.image.getbbox())
  32. if util.isPi():
  33. # TODO PIL version is too old on Pi
  34. self.image = self.image.resize((self.gui.width, self.gui.height))
  35. else:
  36. self.image = self.image.resize((self.gui.width, self.gui.height),
  37. Image.Resampling.NEAREST)
  38. # new image object is also missing these
  39. self.image.is_animated = False
  40. self.image.n_frames = 1
  41. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  42. self.xOff = int((self.gui.width - self.image.width) / 2)
  43. self.yOff = int((self.gui.height - self.image.height) / 2)
  44. self.restart()
  45. def restart(self):
  46. self.start = time.time()
  47. self.frame = time.time()
  48. self.count = 0
  49. self.done = 0
  50. self.image.seek(0)
  51. def finished(self):
  52. if self.done >= self.iterations:
  53. return True
  54. return (time.time() - self.start) >= self.timeout
  55. def draw(self):
  56. if self.image.is_animated:
  57. now = time.time()
  58. if (now - self.frame) >= self.time:
  59. self.frame = now
  60. self.count = (self.count + 1) % self.image.n_frames
  61. if self.count == 0:
  62. self.done += 1
  63. self.image.seek(self.count)
  64. p = self.image.getpalette()
  65. for x in range(0, self.image.width):
  66. for y in range(0, self.image.height):
  67. v = self.image.getpixel((x, y))
  68. if isinstance(v, int):
  69. c = None
  70. if self.background != None:
  71. if "transparency" in self.image.info:
  72. if v == self.image.info["transparency"]:
  73. c = self.background
  74. else:
  75. if v == self.image.info["background"]:
  76. c = self.background
  77. if c == None:
  78. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  79. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  80. else:
  81. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  82. if __name__ == "__main__":
  83. import util
  84. t = util.getTarget()
  85. from manager import Manager
  86. m = Manager(t)
  87. scriptDir = os.path.dirname(os.path.realpath(__file__))
  88. imageDir = os.path.join(scriptDir, "images")
  89. for f in os.listdir(os.fsencode(imageDir)):
  90. filename = os.fsdecode(f)
  91. d = ImageScreen(t, filename)
  92. m.add(d)
  93. if filename != "Favicon.png":
  94. continue
  95. # dump generated image to console, for embedding in Pico script
  96. print()
  97. print("Dumping image to img_tmp.py")
  98. with open("img_tmp.py", "w") as f:
  99. f.write("# Image \"" + filename + "\"" + os.linesep)
  100. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  101. f.write("img_data = [" + os.linesep)
  102. for y in range(0, d.image.height):
  103. f.write(" [" + os.linesep)
  104. for x in range(0, d.image.width):
  105. s = str(d.image.getpixel((x, y)))
  106. f.write(" " + s + "," + os.linesep)
  107. f.write(" ]," + os.linesep)
  108. f.write("]" + os.linesep)
  109. print()
  110. m.restart()
  111. t.loop(m.draw)