How to read multiple images from two different folders one by one using python?

Saloni_C

I am new to python and in a learning stage. I am trying to read multiple images from two different folders in a similar order. Each folder contains 147 images. In one folder there is thermal image (FLIR1271) and second folder contains visible RGB image (FLIR1272) corresponding to thermal image. My task is to read and display images one by one from each folder at the same time. The code should be written in such a way that FLIR1271 should be read first thermal image folder and then go to next folder and read it's corresponding visible image FLIR1272. This process should be applied on rest of the 146 images. I tried something given below:

for i in range(0,10):
thermalimages = [] #list of image filenames
  thermal_dir_path = '/content/Infrared_Thermal_Termination_Images'
  thermaldirFiles = os.listdir(thermal_dir_path) #list of directory files
  thermaldirFiles.sort() #good initial sort but doesnt sort numerically very well
  #*sorted(dirFiles) #sort numerically in ascending order
  for files in thermaldirFiles: 
    if '.jpg' in files:
      thermalimages.append(files)
  print (thermalimages)

  visibleimages = [] #list of image filenames
  visible_dir_path = '/content/Visible_Termination_Images'
  visibledirFiles = os.listdir(visible_dir_path) #list of directory files
  visibledirFiles.sort() #good initial sort but doesnt sort numerically very well
  #*sorted(dirFiles) #sort numerically in ascending order
  for files in visibledirFiles: 
    if '.jpg' in files:
      visibleimages.append(files)
  print (visibleimages)

Unfortunately, it's giving unexpected output:

['FLIR1271.jpg', 'FLIR1273.jpg', 'FLIR1281.jpg', 'FLIR1289.jpg', 'FLIR1293.jpg', 'FLIR1337.jpg', 'FLIR1525.jpg', 'FLIR1527.jpg', 'FLIR1529.jpg', 'FLIR1531.jpg', 'FLIR1703.jpg', 'FLIR1711.jpg', 'FLIR1713.jpg', 'FLIR1715.jpg', 'FLIR1719.jpg', 'FLIR1721.jpg', 'FLIR1723.jpg', 'FLIR1725.jpg', 'FLIR1737.jpg', 'FLIR1739.jpg', 'FLIR1781.jpg', 'FLIR1783.jpg', 'FLIR1787.jpg', 'FLIR1789.jpg', 'FLIR1791.jpg', 'FLIR1793.jpg', 'FLIR1795.jpg', 'FLIR1809.jpg', 'FLIR1811.jpg', 'FLIR1813.jpg', 'FLIR1815.jpg', 'FLIR1817.jpg', 'FLIR1819.jpg', 'FLIR1821.jpg', 'FLIR1833.jpg', 'FLIR1835.jpg', 'FLIR1839.jpg', 'FLIR1841.jpg', 'FLIR1843.jpg', 'FLIR1847.jpg', 'FLIR1859.jpg', 'FLIR1861.jpg', 'FLIR1871.jpg', 'FLIR1887.jpg', 'FLIR1889.jpg', 'FLIR1891.jpg', 'FLIR1893.jpg', 'FLIR1895.jpg', 'FLIR1961.jpg', 'FLIR1963.jpg', 'FLIR1965.jpg', 'FLIR1967.jpg', 'FLIR1969.jpg', 'FLIR1971.jpg', 'FLIR1973.jpg', 'FLIR1975.jpg', 'FLIR1977.jpg', 'FLIR1979.jpg', 'FLIR1987.jpg', 'FLIR1989.jpg', 'FLIR1991.jpg', 'FLIR1993.jpg', 'FLIR1995.jpg', 'FLIR1997.jpg', 'FLIR1999.jpg', 'FLIR2001.jpg', 'FLIR2003.jpg', 'FLIR2005.jpg', 'FLIR2007.jpg', 'FLIR2009.jpg', 'FLIR2011.jpg', 'FLIR2013.jpg', 'FLIR2015.jpg', 'FLIR2017.jpg', 'FLIR2019.jpg', 'FLIR2021.jpg', 'FLIR2023.jpg', 'FLIR2025.jpg', 'FLIR2027.jpg', 'FLIR2031.jpg', 'FLIR2033.jpg', 'FLIR2035.jpg', 'FLIR2037.jpg', 'FLIR2039.jpg', 'FLIR2041.jpg', 'FLIR2043.jpg', 'FLIR2045.jpg', 'FLIR2047.jpg', 'FLIR2049.jpg', 'FLIR2051.jpg', 'FLIR2053.jpg', 'FLIR2055.jpg', 'FLIR2057.jpg', 'FLIR2059.jpg', 'FLIR2061.jpg', 'FLIR2063.jpg', 'FLIR2065.jpg', 'FLIR2067.jpg', 'FLIR2069.jpg', 'FLIR2071.jpg', 'FLIR2073.jpg', 'FLIR2075.jpg', 'FLIR2077.jpg', 'FLIR2079.jpg', 'FLIR2081.jpg', 'FLIR2083.jpg', 'FLIR2085.jpg', 'FLIR2097.jpg', 'FLIR2099.jpg', 'FLIR2101.jpg', 'FLIR2103.jpg', 'FLIR2105.jpg', 'FLIR2107.jpg', 'FLIR2111.jpg', 'FLIR2113.jpg', 'FLIR2115.jpg', 'FLIR2117.jpg', 'FLIR2131.jpg', 'FLIR2133.jpg', 'FLIR2135.jpg', 'FLIR2137.jpg', 'FLIR2139.jpg', 'FLIR2141.jpg', 'FLIR2143.jpg', 'FLIR2145.jpg', 'FLIR2147.jpg', 'FLIR2149.jpg', 'FLIR2151.jpg', 'FLIR2153.jpg', 'FLIR2155.jpg', 'FLIR2157.jpg', 'FLIR2167.jpg', 'FLIR2169.jpg', 'FLIR2171.jpg', 'FLIR2173.jpg', 'FLIR2175.jpg', 'FLIR2177.jpg', 'FLIR2179.jpg', 'FLIR2181.jpg', 'FLIR2183.jpg', 'FLIR2185.jpg', 'FLIR2187.jpg', 'FLIR2189.jpg', 'FLIR2191.jpg', 'FLIR2193.jpg', 'FLIR2195.jpg', 'FLIR2197.jpg']

S.B

Although in your case, (you have fixed length strings, the prefix is also the same) you might get the expected output after sorting, it's better not to compare/sort numbers when they are strings. Always pay attention to how you're sorting the strings containing numbers. i.e "8" is bigger than "12"

And from your explanation about "one by one from each folder at the same time" you need to zip those lists containing file names and iterate through them like:

thermal_files = [f"FLIR127{i}" for i in range(1, 6, 2)]
thermal_files.sort(key=lambda x: int(x.removeprefix("FLIR")))

RGB_files = [f"FLIR127{i}" for i in range(2, 7, 2)]
RGB_files.sort(key=lambda x: int(x.removeprefix("FLIR")))

print(thermal_files)
print(RGB_files)

for thermal, rgb in zip(thermal_files, RGB_files):
    print(f"Do something with thermal: {thermal}")
    print(f"Do something with RGB: {rgb}")
    print("-----------------------------------")

output:

['FLIR1271', 'FLIR1273', 'FLIR1275']
['FLIR1272', 'FLIR1274', 'FLIR1276']
Do something with thermal: FLIR1271
Do something with RGB: FLIR1272
-----------------------------------
Do something with thermal: FLIR1273
Do something with RGB: FLIR1274
-----------------------------------
Do something with thermal: FLIR1275
Do something with RGB: FLIR1276
-----------------------------------

Note: Don't count on the order of the returned list from os.listdir():

The list is in arbitrary order.

Note: if '.jpg' in files: is not a correct way to check the file's extension in general. Use .endswith() instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Saving two images in different folders using only one controller

Python - How to compare files one by one in two different folders

How to read multiple images from multiple folders in python

How to read Images from multiple folders?

Open/read multiple .tif images from multiple folders on different levels

How do I read two folders in a directory and combine them under one label using flow_from_directory?

How to unlink two images from two different folders at once in php

Combine files in multiple folders into one folder on Mac or read multiple files in different folders simultaneously in R

Compare two folders, copy files that are different from one folder to another

How do I sync two folders using bash so a change in one folder gets reflected in a different folder?

How to merge one column from two different file using awk

How to Load multiple angular2 components from different folders in one index.html file?

How to migrate multiple folders in different paths from one git repository to another?

Rsync from multiple folders to one

How to print one answer from two different inputs in Python 3?

Retrieve Multiple images one by one from MySQL database using Python 3

How to run two different classes, having multiple method one by one using xml suite in seleniumwebdriver

Read and extract multiple PDF's from multiple folders using python

How to read two files from one folder?

Displaying random images from multiple folders using python

How to read the contents from two files one by one using foreach loop?

How to copy files from multiple folders into one common folder using a defined list of files in a text file

Modify multiple files in different folders with one command?

Read images from two different folders and list the image directories with name in a csv file

Spring Batch - How to read from One Table and Write Data into two different table

Comparing two different files from Separate Folders with different extensions and opening one with pywinauto

How to merge different images in one

how to get multiple data from two different table from one database in php

How to read two inputs of different data types in one space separated line in python?