【问题标题】:How to fix these IOErrors when creating a thumbnail from a filepath in PIL (Python Imaging Library)从 PIL(Python 图像库)中的文件路径创建缩略图时如何修复这些错误
【发布时间】:2011-06-17 02:45:09
【问题描述】:

我正在尝试在 python 中创建一个简单的函数,它可以接收文件路径和输出文件路径,然后为在文件路径中找到的图像制作 64x64 缩略图并将缩略图保存到输出文件路径。这是我的完整代码:

def create_thumbnail2(filepath, outputpath):
    if not os.path.exists(filepath):
        print "Input file path for create_thumbnail doesn't exist. Returning None"
        return None

    try:
        size = 64, 64 #Will be making a 64x64 thumbnail                                                                                           
        im = Image.open(filepath)
        print "image successfully opened"
        im.thumbnail(size, Image.ANTIALIAS)
        print "made thumbnail"
        im.save(outputpath, "PNG") #Save image as a PNG                                                                                           
        return outputpath
    except IOError:
        print "I/O error"
        return None

print "TEST 1"
filep = "test_images/cat1.jpg"
print create_thumbnail2(filep, "test_images/cat1_thumbnail.png")

print "\nTEST 2"
filep = "test_images/cat2.jpg"
print create_thumbnail2(filep, "test_images/cat2_thumbnail.png")

问题是这段代码对于某些图像可以正常工作,但会在我调用“im.thumbnail(size, Image.ANTIALIAS)”的行上引发 IOError。这是上面程序的输出。

TEST 1
image successfully opened
I/O error
None

TEST 2
image successfully opened
made thumbnail
test_images/cat2_thumbnail.png

您会注意到,在第一个测试中,在打开图像之后但在创建缩略图之前会引发 I/O 错误。在第二次测试中没有抛出错误,并且缩略图实际上成功保存到了输出路径。无论我以什么顺序调用这两个不同的测试,或者如果我将其中一个注释掉并单独运行另一个,结果总是 TEST 1 失败而 TEST 2 成功。 cat1.jpg 和 cat2.jpg 似乎都是有效的 JPEG 图像,除了文件名和实际图片内容之外,我真的找不到它们之间的任何不同之处。

如果有人想用我的图片试试,我从这里下载了 cat1:http://dellone2one.com/wp-content/uploads/2009/11/angry_wet_cat.jpg

我从这里下载了 cat2:http://cvcl.mit.edu/hybrid/cat2.jpg

编辑添加完整的回溯,无需处理: 这是完整的回溯

Traceback (most recent call last):
  File "image_utils.py", line 75, in <module>
    print create_thumbnail2(filep, "test_images/cat1_thumbnail.png")
  File "image_utils.py", line 66, in create_thumbnail2
    im.thumbnail(size, Image.ANTIALIAS)
  File "/Users/dylan/arcode/python/arcode/PIL/Image.py", line 1559, in thumbnail
    self.load()
  File "/Users/dylan/arcode/python/arcode/PIL/ImageFile.py", line 189, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "/Users/dylan/arcode/python/arcode/PIL/Image.py", line 385, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

【问题讨论】:

  • 如果您删除了异常处理并发布了完整的回溯,这将有所帮助。
  • 我的 PIL 版本似乎有问题。你同意吗?

标签: python python-imaging-library ioerror


【解决方案1】:

我的 PIL 安装也是如此。 (PIL 1.1.7、Py 2.6、OSX 10.6)

编辑: 啊 - 在不同的 OSX 机器上安装有效。我知道在 OSX 上构建带有 JPG 支持的 PIL 存在问题,但我不记得这两个安装的来源是什么,所以我无法告诉你如何修复它。

编辑 2: 我最好的回忆是,使用类似here 的说明构建 PIL 产生了工作安装。构建和安装命令必须运行 sudo,并且必须手动修改 gcc 的三个调用以删除“-arch ppc”选项并重新运行。

这里是 another route 来构建 PIL。

【讨论】:

  • 嗯,好的。我很确定我刚刚从 MacPorts 安装了我的
  • 那么在另一个OSX上安装,是不是也是PIL 1.1.7,Py 2.6,OSX 10.6?
  • PIL-1.1.7-py2.6-macosx-10.3-fat.egg,但在 OSX 10.6 上运行。我想我是按照这里的说明自己构建的:passingcuriosity.com/2009/installing-pil-on-mac-os-x-leopard
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 2012-03-05
  • 2010-12-11
相关资源
最近更新 更多