Sib*_*ing 41 python python-imaging-library
我在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')
我最终得到以下错误:
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
为什么会这样,我该如何解决?
正如所建议的,我已经将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
都失败了!
nao*_*oko 66
我有同样的问题.
from PIL import Image
代替
import Image
解决了这个问题
mus*_*akc 15
所以在经历了这个问题很长一段时间之后,这可以帮助你:
from PIL import Image
代替
import Image
此外,如果您的图像文件未加载,并且您收到错误"没有文件或目录",那么您应该这样做:
path=r'C:\ABC\Users\Pictures\image.jpg'
然后打开文件
image=Image.open(path)
就我而言……我的代码中已经有了“from PIL import Image”。
发生错误是因为我的代码中的先前操作仍在使用(锁定)图像文件。我不得不添加一个小的延迟或尝试在循环中以追加模式打开文件,直到没有失败。一旦没有失败,就意味着该文件不再被使用,我可以继续并让 PIL 打开该文件。这是我用来检查文件是否正在使用并等待它可用的函数。
def is_locked(filepath):
    locked = None
    file_object = None
    if os.path.exists(filepath):
        try:
            buffer_size = 8
            # Opening file in append mode and read the first 8 characters.
            file_object = open(filepath, 'a', buffer_size)
            if file_object:
                locked = False
        except IOError as message:
            locked = True
        finally:
            if file_object:
                file_object.close()
    return locked
def wait_for_file(filepath):
    wait_time = 1
    while is_locked(filepath):
        time.sleep(wait_time)
首先,检查你的枕头版本
python -c 'import PIL; print PIL.PILLOW_VERSION'
我使用pip install --upgrade pillow升级版本从2.7到2.9(或3.0)修复此问题.
小智 6
就我而言,图像在下载过程中被损坏(使用 wget 和 github url)。
尝试使用来自不同来源的多个图像。
from PIL import Image 
Image.open()