-1

I am trying to process multiple images in a directory by providing a with statement and a text file listing the file paths for the files I want processed (processing includes grayscaling shown and some de-noising and pixel intensity measures). With the code shown below the program correctly processes the first file on the list but then ends before processing the other files. Does anyone know why and how I can make it iterate through all files listed?

#establish loop
with open('file_list.txt') as inf:
    for line in inf:
       path = line

# grayscale and plot
original = io.imread(path, plugin = 'pil')
grayscale = rgb2gray(original)
B. Leo
  • 3
  • 2
  • 3
    Your code probably processes the last file in the list, not the first one. The last two lines should be indented to be part of the loop. – Thierry Lathuille Nov 18 '19 at 18:56
  • Does this answer your question? [Break is not activated inside a for loop in Python](https://stackoverflow.com/questions/58836678/break-is-not-activated-inside-a-for-loop-in-python) – Michele Dorigatti Nov 18 '19 at 19:07

1 Answers1

1

Every iteration will give a new path, so what you will be getting is the last path, not the first one. Run imread and rgb2gray inside the loop after assigning path.

#establish loop is correct
with open('file_list.txt') as inf:
    for line in inf:
       path = line # Each iteration path will have a new path value

       # grayscale and plot
       original = io.imread(path, plugin = 'pil')
       grayscale = rgb2gray(original)