Image.open()无法识别图像文件 - Python?

时间:2021-08-01 03:25:59

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!

4 个解决方案

#1


63  

I had a same issue.

我有同样的问题。

from PIL import Image

instead of

import Image

fixed the issue

解决了这个问题

#2


14  

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)

#3


3  

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)修复此问题。

#4


1  

If you are using Anaconda on windows then you can open Anaconda Navigator app and go to Environment section and search for pillow in installed libraries and mark it for upgrade to latest version by right clicking on the checkbox.

如果您在Windows上使用Anaconda,那么您可以打开Anaconda Navigator应用程序并转到环境部分并在已安装的库中搜索枕头,并通过右键单击复选框将其标记为升级到最新版本。

Screenshot for reference:Image.open()无法识别图像文件 -  Python?

截图供参考:

This has fixed the following error:

这修复了以下错误:

PermissionError: [WinError 5] Access is denied: 'e:\\work\\anaconda\\lib\\site-packages\\pil\\_imaging.cp36-win_amd64.pyd'

#1


63  

I had a same issue.

我有同样的问题。

from PIL import Image

instead of

import Image

fixed the issue

解决了这个问题

#2


14  

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)

#3


3  

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)修复此问题。

#4


1  

If you are using Anaconda on windows then you can open Anaconda Navigator app and go to Environment section and search for pillow in installed libraries and mark it for upgrade to latest version by right clicking on the checkbox.

如果您在Windows上使用Anaconda,那么您可以打开Anaconda Navigator应用程序并转到环境部分并在已安装的库中搜索枕头,并通过右键单击复选框将其标记为升级到最新版本。

Screenshot for reference:Image.open()无法识别图像文件 -  Python?

截图供参考:

This has fixed the following error:

这修复了以下错误:

PermissionError: [WinError 5] Access is denied: 'e:\\work\\anaconda\\lib\\site-packages\\pil\\_imaging.cp36-win_amd64.pyd'