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
![../_images/9dcb4fa3cc33fd9ba041a53c100899783830c2d6a1502abf8b0d297132229551.png](../_images/9dcb4fa3cc33fd9ba041a53c100899783830c2d6a1502abf8b0d297132229551.png)
banana0003.tif
![../_images/3621bb9a497ae303f915b4560eea3f214db14ddefc8a5470bdb2f9389ffe065e.png](../_images/3621bb9a497ae303f915b4560eea3f214db14ddefc8a5470bdb2f9389ffe065e.png)
banana0004.tif
![../_images/5d1d1886e9743966fc97394fa5366f727f8e096703fc4824aa5d1d3d9384efb2.png](../_images/5d1d1886e9743966fc97394fa5366f727f8e096703fc4824aa5d1d3d9384efb2.png)
banana0005.tif
![../_images/da2a4b8c174d38b50e5304f974c528e9acd6f3710bcc9d2c3f7b3f7b20c68a94.png](../_images/da2a4b8c174d38b50e5304f974c528e9acd6f3710bcc9d2c3f7b3f7b20c68a94.png)
banana0006.tif
![../_images/d7fe86d329fa97ba2028092758922bb1bd033f8f309dcfd3674d242ad779b080.png](../_images/d7fe86d329fa97ba2028092758922bb1bd033f8f309dcfd3674d242ad779b080.png)
banana0007.tif
![../_images/d6f68e3666217141008af4847f4b1b85362ea709588be1f69fa2bba09605ab6d.png](../_images/d6f68e3666217141008af4847f4b1b85362ea709588be1f69fa2bba09605ab6d.png)
banana0008.tif
![../_images/ac715ecda1e10482a91832ce7f486df0e35dde377d3ebaa793b342101be5bc4f.png](../_images/ac715ecda1e10482a91832ce7f486df0e35dde377d3ebaa793b342101be5bc4f.png)
banana0009.tif
![../_images/7f27c907028429272e21918f702ed11bca27bc8586a64f816f02d5a5b09f0946.png](../_images/7f27c907028429272e21918f702ed11bca27bc8586a64f816f02d5a5b09f0946.png)
banana0010.tif
![../_images/3a7999d00484841fc9c5fb948b804342d82c2106c165a1089ea657fc54bf2981.png](../_images/3a7999d00484841fc9c5fb948b804342d82c2106c165a1089ea657fc54bf2981.png)
banana0011.tif
![../_images/1a33eca3b5aa372c6d7c9424f21a1b94315072712ab3a145d05ef4d6777fa03e.png](../_images/1a33eca3b5aa372c6d7c9424f21a1b94315072712ab3a145d05ef4d6777fa03e.png)
banana0012.tif
![../_images/1caae1911b6f111e85860e229a5eb8c20c1120b1254b1a22175b379d3760fdc3.png](../_images/1caae1911b6f111e85860e229a5eb8c20c1120b1254b1a22175b379d3760fdc3.png)
banana0013.tif
![../_images/e012be134a7c4ac946aeac57a9ac7dba445375f393a6588a31d1fa2ca71003a2.png](../_images/e012be134a7c4ac946aeac57a9ac7dba445375f393a6588a31d1fa2ca71003a2.png)
banana0014.tif
![../_images/9a894a5409312539d12102a970f356e96b87ae1606460fbc744b8a0eb0fc37a9.png](../_images/9a894a5409312539d12102a970f356e96b87ae1606460fbc744b8a0eb0fc37a9.png)
banana0015.tif
![../_images/ccff8e4701c90a14e1ac63cff07dc96b75e1f5fbd8f54bceb8e20bde3dd03082.png](../_images/ccff8e4701c90a14e1ac63cff07dc96b75e1f5fbd8f54bceb8e20bde3dd03082.png)
banana0016.tif
![../_images/05fef5370cac110920e8f47013cb56d911f0f450f689bbbb4cba0b436e4ecf70.png](../_images/05fef5370cac110920e8f47013cb56d911f0f450f689bbbb4cba0b436e4ecf70.png)
banana0017.tif
![../_images/f12016eea7b75c7920afd287858fc5c0ea062272e4c11ec64f4c63dc9332165b.png](../_images/f12016eea7b75c7920afd287858fc5c0ea062272e4c11ec64f4c63dc9332165b.png)
banana0018.tif
![../_images/a2c35c905a2d60c15761f5370ec0d791d60654ab72e9d578420cf7859d681429.png](../_images/a2c35c905a2d60c15761f5370ec0d791d60654ab72e9d578420cf7859d681429.png)
banana0019.tif
![../_images/098c999b2e97b7b1f80ae0c30d93439ac415dd0270d402651d502f0e4b70f5c8.png](../_images/098c999b2e97b7b1f80ae0c30d93439ac415dd0270d402651d502f0e4b70f5c8.png)
banana0020.tif
![../_images/c60d9ce1ed9893cf042f62fbf01bb1cfdd3c6e3bb5261404af6357c16f241ba3.png](../_images/c60d9ce1ed9893cf042f62fbf01bb1cfdd3c6e3bb5261404af6357c16f241ba3.png)
banana0021.tif
![../_images/a401c8203e2741e4e5f0ffeb4b2533a09482c8db3531339b200d965698f3cf14.png](../_images/a401c8203e2741e4e5f0ffeb4b2533a09482c8db3531339b200d965698f3cf14.png)
banana0022.tif
![../_images/c883a5b9f046e800704bc5a94f062deb6614a7d2a456cb77f2981eef8ca13534.png](../_images/c883a5b9f046e800704bc5a94f062deb6614a7d2a456cb77f2981eef8ca13534.png)
banana0023.tif
![../_images/b2457b9ec4bd2757d1ed3dfd9fd7bf74bf4b56a0e50327da24c7cd401b289953.png](../_images/b2457b9ec4bd2757d1ed3dfd9fd7bf74bf4b56a0e50327da24c7cd401b289953.png)
banana0024.tif
![../_images/51454523d0863983dba9561b7f0c8086cbc7a5bec03807694708c3dcab34f5c0.png](../_images/51454523d0863983dba9561b7f0c8086cbc7a5bec03807694708c3dcab34f5c0.png)
banana0025.tif
![../_images/8370878803b024aee56ac59c4ec17684b0d0f417e524090d1d87eea279c9b990.png](../_images/8370878803b024aee56ac59c4ec17684b0d0f417e524090d1d87eea279c9b990.png)
banana0026.tif
![../_images/defb3e394cd4a216e5a4e6bafcdb383accf6f4c63eb85047068eab38c089e614.png](../_images/defb3e394cd4a216e5a4e6bafcdb383accf6f4c63eb85047068eab38c089e614.png)
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.