我是 python 新手,處于學習階段。我正在嘗試以類似的順序從兩個不同的檔案夾中讀取多個影像。每個檔案夾包含 147 張影像。在一個檔案夾中有熱影像 (FLIR1271),第二個檔案夾包含與熱影像對應的可見 RGB 影像 (FLIR1272)。我的任務是同時從每個檔案夾中一張一張地讀取和顯示影像。代碼的撰寫方式應該是首先讀取 FLIR1271 熱影像檔案夾,然后轉到下一個檔案夾并讀取其對應的可見影像 FLIR1272。此程序應應用于其余 146 幅影像。我嘗試了下面給出的東西:
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)
不幸的是,它給出了意想不到的輸出:
['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']
uj5u.com熱心網友回復:
盡管在您的情況下,(您有固定長度的字串,前綴也是相同的)您可能會在排序后得到預期的輸出,但最好不要在它們是字串時比較/排序數字。始終注意如何對包含數字的字串進行排序。即"8"大于"12"
從您對“同時從每個檔案夾一個接一個”的解釋中,您需要壓縮那些包含檔案名的串列并像這樣遍歷它們:
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("-----------------------------------")
輸出:
['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
-----------------------------------
注意:不要指望回傳串列的順序os.listdir():
該串列是任意順序的。
注意:if '.jpg' in files:通常不是檢查檔案擴展名的正確方法。改為使用.endswith()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528605.html
標籤:Python循环迭代器
上一篇:限制輸入符號和其他數字(с程式)
下一篇:用亂數擴展變數的長度不起作用
