Skip to content

Commit 7b5ec1f

Browse files
authored
Merge pull request #58 from makermelissa/master
Bug fixes and button improvements to animated Gif Player
2 parents 070c9ec + 71ef6ad commit 7b5ec1f

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

examples/rgb_display_pillow_animated_gif.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,29 @@ def __init__(self, display, width=None, height=None, folder=None):
6666
self.run()
6767

6868
def advance(self, loop=False):
69+
initial_index = self._index
6970
if self._index < len(self._gif_files) - 1:
7071
self._index += 1
7172
elif loop and self._index == len(self._gif_files) - 1:
7273
self._index = 0
74+
return initial_index != self._index
7375

7476
def back(self, loop=False):
77+
initial_index = self._index
7578
if self._index > 0:
7679
self._index -= 1
7780
elif loop and self._index == 0:
7881
self._index = len(self._gif_files) - 1
82+
return initial_index != self._index
7983

8084
def load_files(self, folder):
81-
self._gif_files = [f for f in os.listdir(folder) if f[-4:] == '.gif']
85+
gif_files = [f for f in os.listdir(folder) if f[-4:] == '.gif']
86+
for gif_file in gif_files:
87+
image = Image.open(gif_file)
88+
# Only add animated Gifs
89+
if image.is_animated:
90+
self._gif_files.append(gif_file)
91+
8292
print("Found", self._gif_files)
8393
if not self._gif_files:
8494
print("No Gif files found in current folder")
@@ -133,24 +143,27 @@ def play(self):
133143
# Check if we have loaded any files first
134144
if not self._gif_files:
135145
print("There are no Gif Images to Play")
136-
137-
for frame_object in self._frames:
138-
self.display.image(frame_object.image)
139-
if not self.advance_button.value:
140-
self.advance()
141-
elif not self.back_button.value:
142-
self.back()
143-
time.sleep(frame_object.duration / 1000)
144-
145-
if self._loop == 1:
146-
return
147-
if self._loop > 0:
148-
self._loop -= 1
146+
while True:
147+
for frame_object in self._frames:
148+
self.display.image(frame_object.image)
149+
if not self.advance_button.value:
150+
if self.advance():
151+
return False
152+
elif not self.back_button.value:
153+
if self.back():
154+
return False
155+
time.sleep(frame_object.duration / 1000)
156+
157+
if self._loop == 1:
158+
return True
159+
if self._loop > 0:
160+
self._loop -= 1
149161

150162
def run(self):
151163
while True:
152-
self.play()
153-
self.advance(True)
164+
auto_advance = self.play()
165+
if auto_advance:
166+
self.advance(True)
154167

155168
# Config for display baudrate (default max is 24mhz):
156169
BAUDRATE = 64000000

0 commit comments

Comments
 (0)