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.

image.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # GIF images contain Palletes for each frame.
  15. # These can be different between frames.
  16. # So the library is converting them to RGB.
  17. # This means we can not replace the background easily
  18. # for all but the first frame in animations.
  19. # This setting changes that to keep the same palette, as long as possible.
  20. # This works with some GIFs, but not all of them.
  21. from PIL import GifImagePlugin
  22. GifImagePlugin.LOADING_STRATEGY = GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY
  23. class ImageScreen:
  24. def __init__(self, g, p, t = 0.2, i = 1, to = 5.0, bg = None):
  25. self.gui = g
  26. self.time = t
  27. self.iterations = i
  28. self.timeout = to
  29. self.background = bg
  30. scriptDir = os.path.dirname(os.path.realpath(__file__))
  31. self.path = os.path.join(scriptDir, "images", p)
  32. self.image = Image.open(self.path)
  33. # for some reason non-animated images don't even have this attribute
  34. if not hasattr(self.image, "is_animated"):
  35. self.image.is_animated = False
  36. self.image.n_frames = 1
  37. # automatically crop and scale large images
  38. if not self.image.is_animated and ((self.image.width > self.gui.width) or (self.image.height > self.gui.height)):
  39. # crop to visible area
  40. self.image = self.image.crop(self.image.getbbox())
  41. # keep the aspect ratio and fit within visible area
  42. ratio = self.image.width / self.image.height
  43. max_width = int(ratio * self.gui.height)
  44. max_height = int(self.gui.width / ratio)
  45. if (max_height >= self.gui.height) or (((self.gui.width - max_width) < (self.gui.height - max_height)) and ((self.gui.width - max_width) >= 0)):
  46. width = max_width
  47. height = self.gui.height
  48. else:
  49. width = self.gui.width
  50. height = max_height
  51. # resize
  52. self.image = self.image.resize((width, height),
  53. Image.Resampling.NEAREST)
  54. # new image object is also missing these
  55. self.image.is_animated = False
  56. self.image.n_frames = 1
  57. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  58. self.xOff = int((self.gui.width - self.image.width) / 2)
  59. self.yOff = int((self.gui.height - self.image.height) / 2)
  60. self.restart()
  61. def restart(self):
  62. self.start = time.time()
  63. self.frame = time.time()
  64. self.count = 0
  65. self.done = 0
  66. self.image.seek(0)
  67. def finished(self):
  68. if self.done >= self.iterations:
  69. return True
  70. # only apply timeout for static images
  71. if self.image.is_animated:
  72. return False
  73. else:
  74. return (time.time() - self.start) >= self.timeout
  75. def draw(self):
  76. if self.image.is_animated:
  77. now = time.time()
  78. if (now - self.frame) >= self.time:
  79. self.frame = now
  80. self.count = (self.count + 1) % self.image.n_frames
  81. if self.count == 0:
  82. self.done += 1
  83. self.image.seek(self.count)
  84. p = self.image.getpalette()
  85. for x in range(0, self.image.width):
  86. for y in range(0, self.image.height):
  87. v = self.image.getpixel((x, y))
  88. if isinstance(v, int):
  89. c = None
  90. if self.background != None:
  91. if "transparency" in self.image.info:
  92. if v == self.image.info["transparency"]:
  93. c = self.background
  94. else:
  95. if v == self.image.info["background"]:
  96. c = self.background
  97. if c == None:
  98. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  99. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  100. else:
  101. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  102. if __name__ == "__main__":
  103. import sys
  104. import util
  105. t = util.getTarget()
  106. from manager import Manager
  107. m = Manager(t)
  108. scriptDir = os.path.dirname(os.path.realpath(__file__))
  109. imageDir = os.path.join(scriptDir, "images")
  110. for f in os.listdir(os.fsencode(imageDir)):
  111. filename = os.fsdecode(f)
  112. d = ImageScreen(t, filename)
  113. m.add(d)
  114. if filename != "Favicon.png":
  115. continue
  116. try:
  117. # dump generated image to console, for embedding in Pico script
  118. print()
  119. print("Dumping image to img_tmp.py")
  120. with open("img_tmp.py", "w") as f:
  121. f.write("# Image \"" + filename + "\"" + os.linesep)
  122. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  123. f.write("img_data = [" + os.linesep)
  124. for y in range(0, d.image.height):
  125. f.write(" [" + os.linesep)
  126. for x in range(0, d.image.width):
  127. s = str(d.image.getpixel((x, y)))
  128. f.write(" " + s + "," + os.linesep)
  129. f.write(" ]," + os.linesep)
  130. f.write("]" + os.linesep)
  131. print()
  132. except Exception as e:
  133. print()
  134. if hasattr(sys, "print_exception"):
  135. sys.print_exception(e)
  136. else:
  137. print(e)
  138. print()
  139. m.restart()
  140. util.loop(t, m.draw)