How process files in a folder#
In this notebook we will program a loop which walks over a folder of images. Furthermore, the loop will call a python function that analyses the images one by one. Hence, we will process all images in that folder using the same procedure.
See also
import os
from skimage.io import imread
import stackview
For demonstration purposes, we reuse a folder of images showing banana-slices imaged using magnetic resonance imaging (Courtesy of Nasreddin Abolmaali, OncoRay, TU Dresden)
# define the location of the folder to go through
directory = 'data_banana/'
# get a list of files in that folder
file_list = os.listdir(directory)
file_list
['banana0002.tif',
'banana0003.tif',
'banana0004.tif',
'banana0005.tif',
'banana0006.tif',
'banana0007.tif',
'banana0008.tif',
'banana0009.tif',
'banana0010.tif',
'banana0011.tif',
'banana0012.tif',
'banana0013.tif',
'banana0014.tif',
'banana0015.tif',
'banana0016.tif',
'banana0017.tif',
'banana0018.tif',
'banana0019.tif',
'banana0020.tif',
'banana0021.tif',
'banana0022.tif',
'banana0023.tif',
'banana0024.tif',
'banana0025.tif',
'banana0026.tif',
'image_source.txt']
Obviously, there are not just images in that folder. We can filter that list with a short for-statement:
image_file_list = [file for file in file_list if file.endswith(".tif")]
image_file_list
['banana0002.tif',
'banana0003.tif',
'banana0004.tif',
'banana0005.tif',
'banana0006.tif',
'banana0007.tif',
'banana0008.tif',
'banana0009.tif',
'banana0010.tif',
'banana0011.tif',
'banana0012.tif',
'banana0013.tif',
'banana0014.tif',
'banana0015.tif',
'banana0016.tif',
'banana0017.tif',
'banana0018.tif',
'banana0019.tif',
'banana0020.tif',
'banana0021.tif',
'banana0022.tif',
'banana0023.tif',
'banana0024.tif',
'banana0025.tif',
'banana0026.tif']
Alternatively, we can also write a longer for-loop and check if files are images. This code does exactly the same, it is just written in a different way.
# go through all files in the folder
for file in file_list:
# if the filename is of a tif-image, print it out
if file.endswith(".tif"):
print(file)
banana0002.tif
banana0003.tif
banana0004.tif
banana0005.tif
banana0006.tif
banana0007.tif
banana0008.tif
banana0009.tif
banana0010.tif
banana0011.tif
banana0012.tif
banana0013.tif
banana0014.tif
banana0015.tif
banana0016.tif
banana0017.tif
banana0018.tif
banana0019.tif
banana0020.tif
banana0021.tif
banana0022.tif
banana0023.tif
banana0024.tif
banana0025.tif
banana0026.tif
For reading and showing these images, we introduce a variable within our for loop to store the image.
for file in file_list:
if file.endswith(".tif"):
print(file)
image = imread(directory + file)
stackview.imshow(image)
banana0002.tif
banana0003.tif
banana0004.tif
banana0005.tif
banana0006.tif
banana0007.tif
banana0008.tif
banana0009.tif
banana0010.tif
banana0011.tif
banana0012.tif
banana0013.tif
banana0014.tif
banana0015.tif
banana0016.tif
banana0017.tif
banana0018.tif
banana0019.tif
banana0020.tif
banana0021.tif
banana0022.tif
banana0023.tif
banana0024.tif
banana0025.tif
banana0026.tif
Exercises#
Write code that prints out the number of TIF images in that folder.
Write code that prints out the number of files in that folder which are no TIF images.