|
| 1 | +import os |
| 2 | +import time |
| 3 | +from PIL import Image, ImageOps |
| 4 | + |
| 5 | +# pylint: disable=too-few-public-methods |
| 6 | +class Frame: |
| 7 | + def __init__(self, duration=0): |
| 8 | + self.duration = duration |
| 9 | + self.image = None |
| 10 | + |
| 11 | + |
| 12 | +# pylint: enable=too-few-public-methods |
| 13 | + |
| 14 | + |
| 15 | +class AnimatedGif: |
| 16 | + def __init__(self, display, include_delays=True, folder=None): |
| 17 | + self._frame_count = 0 |
| 18 | + self._loop = 0 |
| 19 | + self._index = 0 |
| 20 | + self._duration = 0 |
| 21 | + self._gif_files = [] |
| 22 | + self._frames = [] |
| 23 | + self._running = True |
| 24 | + self.display = display |
| 25 | + self.include_delays = include_delays |
| 26 | + if folder is not None: |
| 27 | + self.load_files(folder) |
| 28 | + self.run() |
| 29 | + |
| 30 | + def advance(self): |
| 31 | + self._index = (self._index + 1) % len(self._gif_files) |
| 32 | + |
| 33 | + def back(self): |
| 34 | + self._index = (self._index - 1 + len(self._gif_files)) % len(self._gif_files) |
| 35 | + |
| 36 | + def load_files(self, folder): |
| 37 | + gif_files = [f for f in os.listdir(folder) if f.endswith(".gif")] |
| 38 | + gif_folder = folder |
| 39 | + if gif_folder[:-1] != "/": |
| 40 | + gif_folder += "/" |
| 41 | + for gif_file in gif_files: |
| 42 | + image = Image.open(gif_folder + gif_file) |
| 43 | + # Only add animated Gifs |
| 44 | + if image.is_animated: |
| 45 | + self._gif_files.append(gif_folder + gif_file) |
| 46 | + |
| 47 | + print("Found", self._gif_files) |
| 48 | + if not self._gif_files: |
| 49 | + print("No Gif files found in current folder") |
| 50 | + exit() # pylint: disable=consider-using-sys-exit |
| 51 | + |
| 52 | + def preload(self): |
| 53 | + image = Image.open(self._gif_files[self._index]) |
| 54 | + print("Loading {}...".format(self._gif_files[self._index])) |
| 55 | + if "duration" in image.info: |
| 56 | + self._duration = image.info["duration"] |
| 57 | + else: |
| 58 | + self._duration = 0 |
| 59 | + if "loop" in image.info: |
| 60 | + self._loop = image.info["loop"] |
| 61 | + else: |
| 62 | + self._loop = 1 |
| 63 | + self._frame_count = image.n_frames |
| 64 | + self._frames.clear() |
| 65 | + for frame in range(self._frame_count): |
| 66 | + image.seek(frame) |
| 67 | + # Create blank image for drawing. |
| 68 | + # Make sure to create image with mode 'RGB' for full color. |
| 69 | + frame_object = Frame(duration=self._duration) |
| 70 | + if "duration" in image.info: |
| 71 | + frame_object.duration = image.info["duration"] |
| 72 | + frame_object.image = ImageOps.pad( # pylint: disable=no-member |
| 73 | + image.convert("RGB"), |
| 74 | + (self._width, self._height), |
| 75 | + method=Image.NEAREST, |
| 76 | + color=(0, 0, 0), |
| 77 | + centering=(0.5, 0.5), |
| 78 | + ) |
| 79 | + self._frames.append(frame_object) |
| 80 | + |
| 81 | + def play(self): |
| 82 | + self.preload() |
| 83 | + current_frame = 0 |
| 84 | + last_action = None |
| 85 | + # Check if we have loaded any files first |
| 86 | + if not self._gif_files: |
| 87 | + print("There are no Gif Images loaded to Play") |
| 88 | + return False |
| 89 | + self.update_display(self._frames[current_frame].image) |
| 90 | + while self._running: |
| 91 | + action = self.get_next_value() |
| 92 | + if action: |
| 93 | + if not last_action: |
| 94 | + last_action = action |
| 95 | + if action == "click": |
| 96 | + self.advance() |
| 97 | + return False |
| 98 | + elif int(action) < int(last_action): |
| 99 | + current_frame -= 1 |
| 100 | + else: |
| 101 | + current_frame += 1 |
| 102 | + current_frame %= self._frame_count |
| 103 | + frame_object = self._frames[current_frame] |
| 104 | + start_time = time.monotonic() |
| 105 | + self.update_display(frame_object.image) |
| 106 | + if self.include_delays: |
| 107 | + remaining_delay = frame_object.duration / 1000 - ( |
| 108 | + time.monotonic() - start_time |
| 109 | + ) |
| 110 | + if remaining_delay > 0: |
| 111 | + time.sleep(remaining_delay) |
| 112 | + last_action = action |
| 113 | + if self._loop == 1: |
| 114 | + return True |
| 115 | + if self._loop > 0: |
| 116 | + self._loop -= 1 |
| 117 | + |
| 118 | + def run(self): |
| 119 | + while self._running: |
| 120 | + auto_advance = self.play() |
| 121 | + if auto_advance: |
| 122 | + self.advance() |
0 commit comments