python - For loop terminates after first item in python
问题描述
I am trying to process multiple images in a directory by providing a with statement and a text file listing the file paths for the files I want processed (processing includes grayscaling shown and some de-noising and pixel intensity measures). With the code shown below the program correctly processes the first file on the list but then ends before processing the other files. Does anyone know why and how I can make it iterate through all files listed?
#establish loop
with open('file_list.txt') as inf:
for line in inf:
path = line
# grayscale and plot
original = io.imread(path, plugin = 'pil')
grayscale = rgb2gray(original)
解决方案
每次迭代都会给出一个新的path
,所以你将得到的是最后一条路径,而不是第一条。imread
分配路径后rgb2gray
在循环内运行。
#establish loop is correct
with open('file_list.txt') as inf:
for line in inf:
path = line # Each iteration path will have a new path value
# grayscale and plot
original = io.imread(path, plugin = 'pil')
grayscale = rgb2gray(original)
推荐阅读
- loopbackjs - 如何在 Loopback 3 中为自动生成的 SQL 查询添加 NOLOCK
- sql - BigQuery:将数组中的键值对转换为列
- vba - 如何将带有 findwindow api 的 vba 宏转换为 google app 脚本?
- sql - 表中不需要的删除
- javascript - 如何在选择开始日期时自动显示截止日期
- r - 按填充组顺序排列的ggplot2条形图
- javascript - 如何在 React Saga 中检查 API 帖子的成功/错误
- javascript - 如何使用乘法函数执行指数函数?
- c - 存储在光盘中的图像文件中是否存在步幅作为图像参数?
- javascript - Redux-Native 集成未按预期工作