Image.open() 无法识别图像文件 - Python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19230991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image.open() cannot identify image file - Python?
提问by Sibbs Gambling
I am running Python 2.7 in Visual Studio 2013. The code previously worked ok when in Spyder, but when I run:
我在 Visual Studio 2013 中运行 Python 2.7。之前的代码在 Spyder 中运行正常,但是当我运行时:
import numpy as np
import scipy as sp
import math as mt
import matplotlib.pyplot as plt
import Image
import random
# (0, 1) is N
SCALE = 2.2666 # the scale is chosen to be 1 m = 2.266666666 pixels
MIN_LENGTH = 150 # pixels
PROJECT_PATH = 'C:\cimtrack_v1'
im = Image.open(PROJECT_PATH + '\ST.jpg')
I end up with the following errors:
我最终遇到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\cimtrack_v1\PythonApplication1\dr\trajgen.py", line 19, in <module>
im = Image.open(PROJECT_PATH + '\ST.jpg')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2020, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
Why is it so and how may I fix it?
为什么会这样,我该如何解决?
As suggested, I have used the Pillow installer to my Python 2.7. But weirdly, I end up with this:
按照建议,我已经将 Pillow 安装程序用于我的 Python 2.7。但奇怪的是,我最终得到了这个:
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> from pil import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pil
>>> import PIL.Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL.Image
>>> import PIL
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
All fail!
都失败了!
采纳答案by naoko
I had a same issue.
我有同样的问题。
from PIL import Image
instead of
代替
import Image
fixed the issue
修复了问题
回答by musicakc
So after struggling with this issue for quite some time, this is what could help you:
因此,在与此问题斗争了很长一段时间之后,这可以帮助您:
from PIL import Image
instead of
代替
import Image
Also, if your Image file is not loading and you're getting an error "No file or directory" then you should do this:
此外,如果您的图像文件未加载并且您收到错误“无文件或目录”,那么您应该执行以下操作:
path=r'C:\ABC\Users\Pictures\image.jpg'
and then open the file
然后打开文件
image=Image.open(path)
回答by iamsk
first, check your pillow version
首先,检查您的枕头版本
python -c 'import PIL; print PIL.PILLOW_VERSION'
I use pip install --upgrade pillow
upgrade the version from 2.7 to 2.9(or 3.0) fixed this.
我使用pip install --upgrade pillow
将版本从 2.7 升级到 2.9(或 3.0)解决了这个问题。
回答by ThinkFloyd
If you are using Anaconda on windows then you can open Anaconda Navigatorapp and go to Environmentsection and search for pillowin installed libraries and mark it for upgrade to latest version by right clicking on the checkbox.
如果您在 Windows 上使用 Anaconda,那么您可以打开Anaconda Navigator应用程序并转到环境部分并在已安装的库中搜索枕头,并通过右键单击复选框将其标记为升级到最新版本。
This has fixed the following error:
这已修复以下错误:
PermissionError: [WinError 5] Access is denied: 'e:\work\anaconda\lib\site-packages\pil\_imaging.cp36-win_amd64.pyd'
回答by Anoop Sharma
Seems like a Permissions Issue. I was facing the same error. But when I ran it from the root account, it worked. So either give the read permission to the file using chmod (in linux) or run your script after logging in as a root user.
似乎是权限问题。我面临同样的错误。但是当我从 root 帐户运行它时,它起作用了。因此,要么使用 chmod(在 linux 中)授予该文件的读取权限,要么在以 root 用户身份登录后运行您的脚本。
回答by panofish
In my case.. I already had "from PIL import Image" in my code.
就我而言……我的代码中已经有了“from PIL import Image”。
The error occurred for me because the image file was still in use (locked) by a previous operation in my code. I had to add a small delay or attempt to open the file in append mode in a loop, until that did not fail. Once that did not fail, it meant the file was no longer in use and I could continue and let PIL could now open the file.
发生错误是因为我的代码中的先前操作仍在使用(锁定)图像文件。我不得不添加一个小的延迟或尝试在循环中以追加模式打开文件,直到没有失败。一旦没有失败,就意味着该文件不再被使用,我可以继续并让 PIL 现在可以打开该文件。
回答by DavidCH12345
In my case there was an empty picture in the folder. After deleting the empty .jpg's it worked normally.
就我而言,文件夹中有一张空图片。删除空的 .jpg 后,它可以正常工作。
回答by jchmyz
In my case the image file had just been written to and needed to be flushed before opening, like so:
在我的情况下,图像文件刚刚写入并需要在打开前刷新,如下所示:
img_file.flush()
img = Image.open(img_file.name))
回答by pinkie pAI
In my case, it was because the images I used were stored on a Mac, which generates many hidden files like .image_file.png, so they turned out to not even be the actual images I needed and I could safely ignore the warning or delete the hidden files. It was just an oversight in my case.
就我而言,这是因为我使用的图像存储在 Mac 上,这会生成许多隐藏文件,例如 .image_file.png,因此它们甚至不是我需要的实际图像,我可以放心地忽略警告或删除隐藏文件。就我而言,这只是一个疏忽。
回答by Deil
For anyone who make it in bigger scale, you might have also check how many file descriptors you have. It will throw this error if you ran out at bad moment.
对于任何规模更大的人,您可能还检查了您拥有多少个文件描述符。如果你在糟糕的时刻跑出去,它会抛出这个错误。