0

I am trying to make a slide show and have all of the image paths stored in 1 text file to make the main code a bit cleaner, here is the main code :

import tkinter as tk
from itertools import cycle
from PIL import ImageTk, Image

images = open('list.txt', 'r').read()
print(images)

photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)

def slideShow():
  img = next(photos)
  displayCanvas.config(image=img)
  root.after(1200, slideShow) 

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenwidth()
root.geometry('%dx%d' % (1600, 900))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(1000, lambda: slideShow())
root.mainloop()

and here is a download for the list file, in case it needs reformatting or something: https://drive.google.com/open?id=17PzCCf6DK9L-8q4ZxVe7bPD1kFlIuZts

I currently get this error when I try to run the code FileNotFoundError: [Errno 2] No such file or directory: '[' I have tried formatting it in different ways, nothing worked it all just had whatever the first character was and then "no such file or directory"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anytarsier67
  • 33
  • 1
  • 11
  • Seems a line in your file is just `[` – OneCricketeer Dec 02 '19 at 05:55
  • its not , atleast in notepad ++ it is formatted like this `["example1.jpg","example2.jpg","example3.jpg"]` just a lot longer and actual file names – anytarsier67 Dec 02 '19 at 05:58
  • And I think you are calling the file which is not in current working directory – Adam Strauss Dec 02 '19 at 05:59
  • 1
    Is there a reason you're storing a formatted list within the file instead of names on separate lines? – OneCricketeer Dec 02 '19 at 05:59
  • I think @cricket_007 raises a very good point. In any case, couldn’t you use `ast.literal_eval()` for this? You could probably parse it as JSON, too. Also, you should use context managers to handle files. – AMC Dec 02 '19 at 06:33
  • @AlexanderCécile i am very new to this kind of thing , i used the way i knew how to from previous projects, it may not be a good way , but it works. – anytarsier67 Dec 02 '19 at 06:37
  • @anytarsier67 I’m not reprimanding you, don’t worry! :) It’s just a few suggestions. – AMC Dec 02 '19 at 06:38
  • Does this answer your question? [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – AMC Dec 02 '19 at 19:54

5 Answers5

1

Just replace

images = open('list.txt', 'r').read()
print(images)

with

images =[]
with open('list.txt', 'r') as f:
    lines = f.read().strip('[]')
    images = [i.strip("\" ") for i in lines.split(',')]

Your text file is formatted differently. All I did is strip the text file of [] and then split them with , delimiter and then removed the trailing white spaces and the ".
Hope this helps you :)

Debdut Goswami
  • 1,301
  • 12
  • 28
0

You're calling .read() on the file, which loads everything to a string.

Then you're looping over the string, character by character, trying to open the character as an image

If you have names on each line, then you want this

with open('list.txt', 'r') as f:
    images = f.readlines()
    print(images)
    photos = cycle(ImageTk.PhotoImage(Image.open(image.rstrip())) for image in images)

If your file is formatted any differently, you'll need to parse it first into a list of image file names

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
open('list.txt', 'r').read()

This reads the entire file as a single string, without heed to what that string actually looks like.

cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)

This tries to use each element of images for an Image.open call. Since images is a single string (produced by the previous line), its elements are the individual characters of that string (as 1-character strings). Hence '[' as the first of them.

It seems as though you expected to write the string representation of a list to a file, and then get the actual corresponding list back automatically by reading the file. This does not work. You need to actually interpret the file contents to build the list.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

Accidently you created JSON file which you can convert back to Python's list with module json

import json

images = json.loads(open('list.txt').read())

print(images[0])
furas
  • 134,197
  • 12
  • 106
  • 148
0

1.

import json

with open('../resources/list.txt') as list_file:
    list_res = json.load(list_file)

2.

from ast import literal_eval

with open('../resources/list.txt') as list_file:
    list_res = literal_eval(list_file.read())
Community
  • 1
  • 1
AMC
  • 2,642
  • 7
  • 13
  • 35