Skip to content

Commit 96a732b

Browse files
authored
Merge pull request #51 from pimoroni/patch/image_gallery
image_gallery.py: removed use of saved index.txt
2 parents bbce3e5 + b5ff9c9 commit 96a732b

File tree

1 file changed

+32
-84
lines changed

1 file changed

+32
-84
lines changed

examples/image_gallery.py

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -56,47 +56,44 @@
5656
total_image_count = 0
5757

5858
# Store our current location within the user gallery
59-
current_image = 1
59+
current_image = 0
6060

61+
lfsr = 1
62+
tap = 0xdc29
6163

62-
# We might not have enough space to load the entire directory list into RAM
63-
# so we iterate though 'os.ilistdir' and store each file name in a .txt file for later
64-
def create_index(dir):
65-
66-
# check to see if contents.txt exists and skip the indexing if it does.
67-
if not file_exists(dir + "/index.txt"):
68-
69-
f = open(dir + "/index.txt", "a")
70-
71-
for file in os.ilistdir(dir):
72-
# We're only looking for jpeg images, we don't want to index any other file type
73-
if ".jpg" in file[0] or ".jpeg" in file[0]:
74-
f.write(str(file[0]) + "\n")
75-
76-
f.close()
7764

78-
print("'index.txt' generated successfully!")
65+
# Display an error msg on screen and keep it looping
66+
def display_error(text):
67+
while 1:
68+
display.set_pen(BACKGROUND)
69+
display.clear()
70+
display.set_pen(WHITE)
71+
display.text(f"Error: {text}", 10, 10, WIDTH - 10, 1)
72+
presto.update()
73+
time.sleep(1)
7974

80-
else:
81-
print("\nindex.txt already generated! \nIf you have changed the files in the current directory you will need to delete 'content.txt' and run the program again.\n\n")
8275

76+
try:
77+
# Setup for SD Card
78+
sd_spi = machine.SPI(0, sck=machine.Pin(34, machine.Pin.OUT), mosi=machine.Pin(35, machine.Pin.OUT), miso=machine.Pin(36, machine.Pin.OUT))
79+
sd = sdcard.SDCard(sd_spi, machine.Pin(39))
8380

84-
def count_files(dir):
85-
c = 0
86-
for file in os.ilistdir(dir):
87-
c += 1
88-
return c - 1
81+
# Mount the SD to the directory 'sd'
82+
uos.mount(sd, "/sd")
83+
except OSError:
84+
display_error("Unable to mount SD card")
8985

9086

91-
# Delete the current index file if it exists
92-
# This will force it to recreate it on the next boot.
93-
def delete_index(dir):
94-
print("Removing 'index.txt'")
95-
os.remove(dir + "/index.txt")
87+
def numberedfiles(k):
88+
try:
89+
return int(k[:-4])
90+
except ValueError:
91+
pass
92+
return 0
9693

9794

98-
lfsr = 1
99-
tap = 0xdc29
95+
files = list(sorted(os.listdir("sd/gallery"), key=numberedfiles))
96+
total_image_count = len(files) - 1
10097

10198

10299
def return_point():
@@ -145,22 +142,18 @@ def show_image(show_next=False, show_previous=False):
145142
if current_image < total_image_count:
146143
current_image += 1
147144
else:
148-
current_image = 1
145+
current_image = 0
149146
if show_previous:
150-
if current_image > 1:
147+
if current_image > 0:
151148
current_image -= 1
152149
else:
153150
current_image = total_image_count
154151

155152
# Open the index file and read lines until we're at the correct position
156153
try:
157-
f = open(DIR + "/index.txt", 'r')
158-
for i in range(current_image - 1):
159-
f.readline()
160-
file = f.readline()
161-
f.close()
154+
img = f"sd/gallery/{files[current_image]}"
162155

163-
j.open_file(f"{DIR}/{file}")
156+
j.open_file(img)
164157

165158
img_height, img_width = j.get_height(), j.get_width()
166159

@@ -198,51 +191,6 @@ def clear():
198191
display.clear()
199192

200193

201-
# Display an error msg on screen and keep it looping
202-
def display_error(text):
203-
while 1:
204-
display.set_pen(BACKGROUND)
205-
display.clear()
206-
display.set_pen(WHITE)
207-
display.text(f"Error: {text}", 10, 10, WIDTH - 10, 1)
208-
presto.update()
209-
time.sleep(1)
210-
211-
212-
try:
213-
# Setup for SD Card
214-
sd_spi = machine.SPI(0, sck=machine.Pin(34, machine.Pin.OUT), mosi=machine.Pin(35, machine.Pin.OUT), miso=machine.Pin(36, machine.Pin.OUT))
215-
sd = sdcard.SDCard(sd_spi, machine.Pin(39))
216-
217-
# Mount the SD to the directory 'sd'
218-
uos.mount(sd, "/sd")
219-
except OSError:
220-
display_error("Unable to mount SD card")
221-
222-
223-
# Function to check if a file is present on the filesystem
224-
def file_exists(filename):
225-
try:
226-
return (os.stat(filename)[0] & 0x4000) == 0
227-
except OSError:
228-
return False
229-
230-
231-
try:
232-
# Delete the index to force it to recreate the file in the next step
233-
delete_index(DIR)
234-
except OSError:
235-
print("Unable to delete index")
236-
237-
# Create the index
238-
# And count the images
239-
try:
240-
create_index(DIR)
241-
total_image_count = count_files(DIR)
242-
except OSError:
243-
display_error("Unable to create index file. \nCheck that your file names do not contain non unicode characters.")
244-
245-
246194
# Store the last time the screen was updated
247195
last_updated = time.time()
248196

0 commit comments

Comments
 (0)