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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if len(sys.argv) > 1:
  113. if not sys.argv[1] in filename:
  114. continue
  115. d = ImageScreen(t, filename)
  116. m.add(d)
  117. if filename != "Favicon.png":
  118. continue
  119. try:
  120. # dump generated image to console, for embedding in Pico script
  121. print()
  122. print("Dumping image to img_tmp.py")
  123. with open("img_tmp.py", "w") as f:
  124. f.write("# Image \"" + filename + "\"" + os.linesep)
  125. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  126. f.write("img_data = [" + os.linesep)
  127. for y in range(0, d.image.height):
  128. f.write(" [" + os.linesep)
  129. for x in range(0, d.image.width):
  130. s = str(d.image.getpixel((x, y)))
  131. f.write(" " + s + "," + os.linesep)
  132. f.write(" ]," + os.linesep)
  133. f.write("]" + os.linesep)
  134. print()
  135. except Exception as e:
  136. print()
  137. if hasattr(sys, "print_exception"):
  138. sys.print_exception(e)
  139. else:
  140. print(e)
  141. print()
  142. m.restart()
  143. util.loop(t, m.draw)