首页 > 解决方案 > 在python中解析包含图像的文件时出现EOF

问题描述

我不明白为什么我在这里收到 EOF 错误。有人可以帮忙吗?在这里,我正在尝试遍历包含 3 个要显示的图像的文件夹“Flowers”,但无论如何我都会收到 EOF 错误

这是发生的错误

此外,当我仅显示 1 张图像时,未注释的 2 行代码有效。我想显示所有图像,我该怎么做?

标签: pythonimageeofcv2

解决方案


WHY : EOF 错误是由缺少for-loop块引起的。a 的语法for-loop是:

for item in collection:
  # do something here..
  # do more stuff here..

或者如果for-loop为空,则需要指定pass.

for item in collection:
 #do nothing
 pass

在您的代码中,没有要执行的内容,因此,它需要一个pass关键字。


目标:说了以上,我认为这就是你想要做的:

image_path = "C:\\Users\PC\\Desktop\\Flowers\\{}"

images = ["img1.jpg", "img2.jpg", "img3.jpg"]
for image_name in images:
  image = Image.open(image_path.format(image_name))
  image.show()

推荐阅读