【问题标题】:PIL Image.open() cannot be opened for readingPIL Image.open() 无法打开读取
【发布时间】:2023-03-30 11:08:01
【问题描述】:

收到图像文件后,我无法读取它。

with open(fullpath, 'wb+') as f:
    f.write(filevalue)
f.close()

try:
    im = Image.open(fullpath, 'r')
    im = im.resize((28, 28))
    os.remove(fullpath)
    im.save(fullpath, 'PNG')
except:
    print 'Received non-image file, skipping...'

首先打开完整路径并写入接收到的文件值,然后关闭文件。现在文件在磁盘上。

之后,我打开收到的文件。我检查了这个图像文件没有损坏,一个独立的python脚本可以用PIL Image类打开它。但是,如果我在收到后立即附加打开文件代码,则会出现错误:

File xxxxxx.png could not be opened for reading

既然文件已经写入磁盘,为什么我不能打开它?

【问题讨论】:

  • @paperfish 你检查文件的权限了吗?
  • @mkHun 是的,这个程序可以访问。我又复制了一份,打开文件,没关系。临时解决方案。

标签: python file-io python-imaging-library ioerror


【解决方案1】:

我制作了这个文件的另一个副本,并成功打开了它。这是一个临时解决方案,尽管开销很大。即使我调用了 f.close(),文件似乎也被锁定了,但没有退出整个程序。

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2020-03-10
    • 2011-11-19
    • 2013-09-26
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多