【问题标题】:EOF while parsing a file containing images in python在python中解析包含图像的文件时出现EOF
【发布时间】:2019-07-03 22:19:00
【问题描述】:

我不明白为什么会出现 EOF 错误。有人可以帮忙吗?here I am trying to iterate over a folder 'Flowers' containing 3 images which I want to display but I get EOF error no matter what

this is the error occuring

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

【问题讨论】:

  • 对不起,如果问题被问得不好
  • 你为什么使用截图而不是在帖子中提供代码?也许我不明白我所看到的,但似乎错误只是由于您的 Python 代码有一个没有正文块的“for”语句。好像是说已经到了文件末尾,没有找到块。
  • 是的,这是python源码的语法错误,不是解析图片文件的错误。
  • 好的,我把代码贴出来
  • '''for img1 in images: img = cv2.imread(img1, 1) re = cv2.resize(img, (400, 400)) cv2.imshow("Checking",re) cv2.imwrite("resized_"+img1,re)'''

标签: python image eof cv2


【解决方案1】:

为什么:EOF 错误是由缺少for-loop 块引起的。 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()

【讨论】:

  • 谢谢。但是当我尝试运行 for 循环时,它给了我 EOF 错误。
  • 请更具体地发表您的评论,或重新创建它。
猜你喜欢
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2013-04-25
相关资源
最近更新 更多