Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

image.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if (time.time() - self.frame) >= self.time:
  58. self.frame = time.time()
  59. self.count = (self.count + 1) % self.image.n_frames
  60. if self.count == 0:
  61. self.done += 1
  62. self.image.seek(self.count)
  63. p = self.image.getpalette()
  64. for x in range(0, self.image.width):
  65. for y in range(0, self.image.height):
  66. v = self.image.getpixel((x, y))
  67. if isinstance(v, int):
  68. c = None
  69. if self.background != None:
  70. if "transparency" in self.image.info:
  71. if v == self.image.info["transparency"]:
  72. c = self.background
  73. else:
  74. if v == self.image.info["background"]:
  75. c = self.background
  76. if c == None:
  77. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  78. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  79. else:
  80. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  81. if __name__ == "__main__":
  82. import util
  83. t = util.getTarget()
  84. from manager import Manager
  85. m = Manager(t)
  86. scriptDir = os.path.dirname(os.path.realpath(__file__))
  87. imageDir = os.path.join(scriptDir, "images")
  88. for f in os.listdir(os.fsencode(imageDir)):
  89. filename = os.fsdecode(f)
  90. m.add(ImageScreen(t, filename))
  91. m.restart()
  92. t.loop(m.draw)