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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # enlarge small images
  58. if not self.image.is_animated and ((self.image.width * 2) <= self.gui.width) and ((self.image.height * 2) <= self.gui.height):
  59. self.image = self.image.crop(self.image.getbbox())
  60. self.image = self.image.resize((self.image.width * 2, self.image.height * 2),
  61. Image.Resampling.NEAREST)
  62. self.image.is_animated = False
  63. self.image.n_frames = 1
  64. # TODO cropping and scaling not working for GIF animations
  65. print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
  66. self.xOff = int((self.gui.width - self.image.width) / 2)
  67. self.yOff = int((self.gui.height - self.image.height) / 2)
  68. self.restart()
  69. def restart(self):
  70. self.start = time.time()
  71. self.frame = time.time()
  72. self.count = 0
  73. self.done = 0
  74. self.image.seek(0)
  75. def finished(self):
  76. if self.done >= self.iterations:
  77. return True
  78. # only apply timeout for static images
  79. if self.image.is_animated:
  80. return False
  81. else:
  82. return (time.time() - self.start) >= self.timeout
  83. def draw(self):
  84. if self.image.is_animated:
  85. now = time.time()
  86. if (now - self.frame) >= self.time:
  87. self.frame = now
  88. self.count = (self.count + 1) % self.image.n_frames
  89. if self.count == 0:
  90. self.done += 1
  91. self.image.seek(self.count)
  92. p = self.image.getpalette()
  93. for x in range(0, self.image.width):
  94. for y in range(0, self.image.height):
  95. v = self.image.getpixel((x, y))
  96. if isinstance(v, int):
  97. c = None
  98. if self.background != None:
  99. if "transparency" in self.image.info:
  100. if v == self.image.info["transparency"]:
  101. c = self.background
  102. else:
  103. if v == self.image.info["background"]:
  104. c = self.background
  105. if c == None:
  106. c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
  107. self.gui.set_pixel(x + self.xOff, y + self.yOff, c)
  108. else:
  109. self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
  110. if __name__ == "__main__":
  111. import sys
  112. import util
  113. t = util.getTarget()
  114. from manager import Manager
  115. m = Manager(t)
  116. scriptDir = os.path.dirname(os.path.realpath(__file__))
  117. imageDir = os.path.join(scriptDir, "images")
  118. for f in os.listdir(os.fsencode(imageDir)):
  119. filename = os.fsdecode(f)
  120. if len(sys.argv) > 1:
  121. if not sys.argv[1] in filename:
  122. continue
  123. d = ImageScreen(t, filename)
  124. m.add(d)
  125. if filename != "Favicon.png":
  126. continue
  127. if (t.width != 32) or (t.height != 32):
  128. continue
  129. try:
  130. # dump generated image to console, for embedding in Pico script
  131. print()
  132. print("Dumping image to img_tmp.py")
  133. with open("img_tmp.py", "w") as f:
  134. f.write("# Image \"" + filename + "\"" + os.linesep)
  135. f.write("# size:" + str(d.image.width) + "x" + str(d.image.height) + os.linesep)
  136. f.write("img_data = [" + os.linesep)
  137. for y in range(0, d.image.height):
  138. f.write(" [" + os.linesep)
  139. for x in range(0, d.image.width):
  140. s = str(d.image.getpixel((x, y)))
  141. f.write(" " + s + "," + os.linesep)
  142. f.write(" ]," + os.linesep)
  143. f.write("]" + os.linesep)
  144. print()
  145. except Exception as e:
  146. print()
  147. if hasattr(sys, "print_exception"):
  148. sys.print_exception(e)
  149. else:
  150. print(e)
  151. print()
  152. m.restart()
  153. util.loop(t, m.draw)