Skip to content

Commit b5ff9c9

Browse files
committed
image_gallery.py: removed use of saved index.txt
1 parent 63808c1 commit b5ff9c9

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
@@ -52,47 +52,44 @@
5252
total_image_count = 0
5353

5454
# Store our current location within the user gallery
55-
current_image = 1
55+
current_image = 0
5656

57+
lfsr = 1
58+
tap = 0xdc29
5759

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

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

76-
else:
77-
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")
7871

72+
try:
73+
# Setup for SD Card
74+
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))
75+
sd = sdcard.SDCard(sd_spi, machine.Pin(39))
7976

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

8682

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

9390

94-
lfsr = 1
95-
tap = 0xdc29
91+
files = list(sorted(os.listdir("sd/gallery"), key=numberedfiles))
92+
total_image_count = len(files) - 1
9693

9794

9895
def return_point():
@@ -141,22 +138,18 @@ def show_image(show_next=False, show_previous=False):
141138
if current_image < total_image_count:
142139
current_image += 1
143140
else:
144-
current_image = 1
141+
current_image = 0
145142
if show_previous:
146-
if current_image > 1:
143+
if current_image > 0:
147144
current_image -= 1
148145
else:
149146
current_image = total_image_count
150147

151148
# Open the index file and read lines until we're at the correct position
152149
try:
153-
f = open(DIR + "/index.txt", 'r')
154-
for i in range(current_image - 1):
155-
f.readline()
156-
file = f.readline()
157-
f.close()
150+
img = f"sd/gallery/{files[current_image]}"
158151

159-
j.open_file(f"{DIR}/{file}")
152+
j.open_file(img)
160153

161154
img_height, img_width = j.get_height(), j.get_width()
162155

@@ -194,51 +187,6 @@ def clear():
194187
display.clear()
195188

196189

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

0 commit comments

Comments
 (0)