首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python逐个从两个不同的文件夹读取多个图像?

如何使用python逐个从两个不同的文件夹读取多个图像?
EN

Stack Overflow用户
提问于 2022-11-04 12:13:14
回答 1查看 27关注 0票数 1

我对python还不熟悉,还处于学习阶段。我试图以类似的顺序从两个不同的文件夹读取多个图像。每个文件夹包含147幅图像。在一个文件夹中有热图像(FLIR1271),第二个文件夹包含与热图像相对应的可见RGB图像(FLIR1272)。我的任务是同时从每个文件夹中逐个读取和显示图像。代码应该以这样的方式编写:首先读取FLIR1271热图像文件夹,然后转到下一个文件夹并读取它对应的可见图像FLIR1272。此过程应应用于146幅图像的其余部分。我尝试了下面的一些内容:

代码语言:javascript
复制
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”、“IR1725.jpg”、“17721.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”、“IR1819.jpg”、“FLIR1821.jpg”、“FLIR1833.jpg”、“FLIR1835.jpg”、“IR1839.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”、“IR1973.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”、“IR2045.jpg”、“FLIR2047.jpg”、“FLIR2049.jpg”、“FLIR2051.jpg”、“IR2053.jpg”,“FLIR2055.jpg”、“FLIR2057.jpg”、“FLIR2059.jpg”、“FLIR2061.jpg”、“FLIR2063.jpg”、“FLIR2065.jpg”、“FLIR2067.jpg”、“FLIR2069.jpg”、“FLIR2069.jpg”、“FLIR2071.jpg”、“FLIR2073.jpg”、“FLIR2075.jpg”、“FLIR2077.jpg”、“IR2079.jpg”、“FLIR2081.jpg”、“97.IR2083.jpg”、“FLIR2085.jpg”、“20jpg”、“FLIR2075.jpg”、“FLIR2077.jpg”、“IR2079.jpg”、“FLIR2081.jpg”、“97.IR2083.jpg”、“FLIR2085.jpg”、“20jpg”、“FLIR2075.jpg”、“FLIR2077.jpg”、“IR2079.jpg”、“FLIR2081.jpg”、“97.IR2083.3pg”、“FLIR2085.jpg”、“20jpg”、“FLIR2073.jpg”、“FLIR2075.jpg”、“FLIR2055.jpg”、“FLIR2055.jpg”、“FLIR2065.jpg”、“FLIR20“FLIR2101.jpg”、“FLIR2103.jpg”、“FLIR2105.jpg”、“FLIR2107.jpg”、“FLIR2111.jpg”、“FLIR2113.jpg”、“FLIR2115.jpg”、“FLIR2117.jpg”、“FLIR2131.jpg”、“FLIR2133.jpg”、“FLIR2135.jpg”、“FLIR2137.jpg”、“IR2139.jpg”、“FLIR2141.jpg”、“FLIR2143.jpg”、“FLIR2145.jpg”、“”FLIR21427.jpg“、”FLIR1429.jpg“、”FLIR2141.jpg“、”FLIR2137.jpg“、”FLIR2135.jpg“、”FLIR2101.jpg“、”FLIR2113.jpg“、”FLIR2115.jpg“、”FLIR2141.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“、”FLIR2191.jpg“、”FLIR2151.jpg“、”“FLIR2195.jpg”,“FLIR2197.jpg”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-04 12:31:59

虽然在您的例子中,(您有固定长度的字符串,前缀也是相同的)您可能在排序后得到预期的输出,最好不要比较/排序数字当它们是字符串。始终要注意如何对包含数字的字符串进行排序。也就是说,"8""12"

根据您对“同时从每个文件夹逐个”的解释,您需要压缩包含文件名的列表,并在其中迭代如下:

代码语言:javascript
复制
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("-----------------------------------")

产出:

代码语言:javascript
复制
['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()代替。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74316898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档