15
15
import time
16
16
import digitalio
17
17
import board
18
- from PIL import Image
18
+ from PIL import Image , ImageDraw
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
@@ -34,14 +34,30 @@ def init_button(pin):
34
34
button .pull = digitalio .Pull .UP
35
35
return button
36
36
37
+ # pylint: disable=too-few-public-methods
38
+ class Frame :
39
+ def __init__ (self , duration = 0 ):
40
+ self .duration = duration
41
+ self .image = None
42
+ # pylint: enable=too-few-public-methods
43
+
37
44
class AnimatedGif :
38
- def __init__ (self , display , folder = None ):
45
+ def __init__ (self , display , width = None , height = None , folder = None ):
39
46
self ._frame_count = 0
40
47
self ._loop = 0
41
48
self ._index = 0
42
- self ._delay = 0
49
+ self ._duration = 0
43
50
self ._gif_files = []
44
51
self ._frames = []
52
+
53
+ if width is not None :
54
+ self ._width = width
55
+ else :
56
+ self ._width = disp .width
57
+ if height is not None :
58
+ self ._height = height
59
+ else :
60
+ self ._height = disp .height
45
61
self .display = display
46
62
self .advance_button = init_button (board .D17 )
47
63
self .back_button = init_button (board .D22 )
@@ -71,22 +87,45 @@ def load_files(self, folder):
71
87
def preload (self ):
72
88
image = Image .open (self ._gif_files [self ._index ])
73
89
print ("Loading {}..." .format (self ._gif_files [self ._index ]))
74
- self ._delay = image .info ['duration' ]
90
+ if "duration" in image .info :
91
+ self ._duration = image .info ['duration' ]
92
+ else :
93
+ self ._duration = 0
75
94
if "loop" in image .info :
76
95
self ._loop = image .info ['loop' ]
77
96
else :
78
97
self ._loop = 1
79
98
self ._frame_count = image .n_frames
99
+ screen_ratio = self ._width / self ._height
100
+ image_ratio = image .width / image .height
101
+ if screen_ratio > image_ratio :
102
+ scaled_width = image .width * self ._height // image .height
103
+ scaled_height = self ._height
104
+ else :
105
+ scaled_width = self ._width
106
+ scaled_height = image .height * self ._width // image .width
80
107
81
108
self ._frames .clear ()
82
109
for frame in range (self ._frame_count ):
83
110
image .seek (frame )
84
111
# Create blank image for drawing.
85
112
# Make sure to create image with mode 'RGB' for full color.
86
- frame_image = Image .new ('RGB' , (width , height ))
87
- frame_image .paste (image , (width // 2 - image .width // 2 ,
88
- height // 2 - image .height // 2 ))
89
- self ._frames .append (frame_image )
113
+ frame_object = Frame (duration = self ._duration )
114
+ if "duration" in image .info :
115
+ frame_object .duration = image .info ['duration' ]
116
+ frame_object .image = Image .new ('RGB' , (self ._width , self ._height ))
117
+
118
+ # Get drawing object to draw on image.
119
+ draw = ImageDraw .Draw (frame_object .image )
120
+ draw .rectangle ((0 , 0 , self ._width , self ._height ), outline = 0 , fill = (0 , 0 , 0 ))
121
+
122
+ frame_image = image .copy ()
123
+ frame_image = frame_image .resize ((scaled_width , scaled_height ), Image .BICUBIC )
124
+ x = scaled_width // 2 - self ._width // 2
125
+ y = scaled_height // 2 - self ._height // 2
126
+ frame_image = frame_image .crop ((x , y , x + self ._width , y + self ._height ))
127
+ frame_object .image .paste (frame_image )
128
+ self ._frames .append (frame_object )
90
129
91
130
def play (self ):
92
131
self .preload ()
@@ -95,13 +134,13 @@ def play(self):
95
134
if not self ._gif_files :
96
135
print ("There are no Gif Images to Play" )
97
136
98
- for frame_image in self ._frames :
99
- self .display .image (frame_image )
137
+ for frame_object in self ._frames :
138
+ self .display .image (frame_object . image )
100
139
if not self .advance_button .value :
101
140
self .advance ()
102
141
elif not self .back_button .value :
103
142
self .back ()
104
- time .sleep (self . _delay / 1000 )
143
+ time .sleep (frame_object . duration / 1000 )
105
144
106
145
if self ._loop == 1 :
107
146
return
@@ -136,10 +175,10 @@ def run(self):
136
175
# pylint: enable=line-too-long
137
176
138
177
if disp .rotation % 180 == 90 :
139
- height = disp .width # we swap height/width to rotate it to landscape!
140
- width = disp .height
178
+ disp_height = disp .width # we swap height/width to rotate it to landscape!
179
+ disp_width = disp .height
141
180
else :
142
- width = disp .width # we swap height/width to rotate it to landscape!
143
- height = disp .height
181
+ disp_width = disp .width # we swap height/width to rotate it to landscape!
182
+ disp_height = disp .height
144
183
145
- gif_player = AnimatedGif (disp , "." )
184
+ gif_player = AnimatedGif (disp , width = disp_width , height = disp_height , folder = "." )
0 commit comments