15
15
import time
16
16
import digitalio
17
17
import board
18
- from PIL import Image , ImageDraw
18
+ from PIL import Image , ImageOps
19
19
import adafruit_rgb_display .ili9341 as ili9341
20
20
import adafruit_rgb_display .st7789 as st7789 # pylint: disable=unused-import
21
21
import adafruit_rgb_display .hx8357 as hx8357 # pylint: disable=unused-import
@@ -59,36 +59,26 @@ def __init__(self, display, width=None, height=None, folder=None):
59
59
if width is not None :
60
60
self ._width = width
61
61
else :
62
- self ._width = disp .width
62
+ self ._width = display .width
63
63
if height is not None :
64
64
self ._height = height
65
65
else :
66
- self ._height = disp .height
66
+ self ._height = display .height
67
67
self .display = display
68
68
self .advance_button = init_button (BUTTON_NEXT )
69
69
self .back_button = init_button (BUTTON_PREVIOUS )
70
70
if folder is not None :
71
71
self .load_files (folder )
72
72
self .run ()
73
73
74
- def advance (self , loop = False ):
75
- initial_index = self ._index
76
- if self ._index < len (self ._gif_files ) - 1 :
77
- self ._index += 1
78
- elif loop and self ._index == len (self ._gif_files ) - 1 :
79
- self ._index = 0
80
- return initial_index != self ._index
81
-
82
- def back (self , loop = False ):
83
- initial_index = self ._index
84
- if self ._index > 0 :
85
- self ._index -= 1
86
- elif loop and self ._index == 0 :
87
- self ._index = len (self ._gif_files ) - 1
88
- return initial_index != self ._index
74
+ def advance (self ):
75
+ self ._index = (self ._index + 1 ) % len (self ._gif_files )
76
+
77
+ def back (self ):
78
+ self ._index = (self ._index - 1 + len (self ._gif_files )) % len (self ._gif_files )
89
79
90
80
def load_files (self , folder ):
91
- gif_files = [f for f in os .listdir (folder ) if f [ - 4 :] == '.gif' ]
81
+ gif_files = [f for f in os .listdir (folder ) if f . endswith ( '.gif' ) ]
92
82
for gif_file in gif_files :
93
83
image = Image .open (gif_file )
94
84
# Only add animated Gifs
@@ -112,15 +102,6 @@ def preload(self):
112
102
else :
113
103
self ._loop = 1
114
104
self ._frame_count = image .n_frames
115
- screen_ratio = self ._width / self ._height
116
- image_ratio = image .width / image .height
117
- if screen_ratio > image_ratio :
118
- scaled_width = image .width * self ._height // image .height
119
- scaled_height = self ._height
120
- else :
121
- scaled_width = self ._width
122
- scaled_height = image .height * self ._width // image .width
123
-
124
105
self ._frames .clear ()
125
106
for frame in range (self ._frame_count ):
126
107
image .seek (frame )
@@ -129,18 +110,11 @@ def preload(self):
129
110
frame_object = Frame (duration = self ._duration )
130
111
if "duration" in image .info :
131
112
frame_object .duration = image .info ['duration' ]
132
- frame_object .image = Image .new ('RGB' , (self ._width , self ._height ))
133
-
134
- # Get drawing object to draw on image.
135
- draw = ImageDraw .Draw (frame_object .image )
136
- draw .rectangle ((0 , 0 , self ._width , self ._height ), outline = 0 , fill = (0 , 0 , 0 ))
137
-
138
- frame_image = image .copy ()
139
- frame_image = frame_image .resize ((scaled_width , scaled_height ), Image .BICUBIC )
140
- x = scaled_width // 2 - self ._width // 2
141
- y = scaled_height // 2 - self ._height // 2
142
- frame_image = frame_image .crop ((x , y , x + self ._width , y + self ._height ))
143
- frame_object .image .paste (frame_image )
113
+ frame_object .image = ImageOps .pad (image .convert ("RGB" ),
114
+ (self ._width , self ._height ),
115
+ method = Image .NEAREST ,
116
+ color = (0 , 0 , 0 ),
117
+ centering = (0.5 , 0.5 ))
144
118
self ._frames .append (frame_object )
145
119
146
120
def play (self ):
@@ -154,11 +128,11 @@ def play(self):
154
128
for frame_object in self ._frames :
155
129
self .display .image (frame_object .image )
156
130
if not self .advance_button .value :
157
- if self .advance ():
158
- return False
159
- elif not self .back_button .value :
160
- if self .back ():
161
- return False
131
+ self .advance ()
132
+ return False
133
+ if not self .back_button .value :
134
+ self .back ()
135
+ return False
162
136
time .sleep (frame_object .duration / 1000 )
163
137
164
138
if self ._loop == 1 :
@@ -170,7 +144,7 @@ def run(self):
170
144
while True :
171
145
auto_advance = self .play ()
172
146
if auto_advance :
173
- self .advance (True )
147
+ self .advance ()
174
148
175
149
# Config for display baudrate (default max is 64mhz):
176
150
BAUDRATE = 64000000
0 commit comments