【发布时间】: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