从 PIL(Python 成像库)中的文件路径创建缩略图时如何修复这些 IOErrors

发布于 2024-11-15 12:37:56 字数 2600 浏览 4 评论 0原文

我正在尝试在 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

I am trying to make a simple function in python that can take in a filepath and an outputfilepath, and then make a 64x64 thumbnail for the image found at filepath and save the thumbnail to outputfilepath. Here is my entire code:

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")

The problem is that this code will work fine for some images, but will raise an IOError on the line where I call "im.thumbnail(size, Image.ANTIALIAS)". Here is the output of the above program.

TEST 1
image successfully opened
I/O error
None

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

You'll notice that in the first test, the an I/O Error is thrown after the image is opened but before a thumbnail is created. In the second test no error is thrown, and the thumbnail is actually successfully saved to the outputpath. No matter what order I call the two different tests in, or if I comment one out and run the other one alone, the result is always TEST 1 failing and TEST 2 succeeding. Both cat1.jpg and cat2.jpg seem to be valid JPEG images, I can't really find anything different between them besides the file names and the actual picture content.

In case anyone wants to try it with my images, I downloaded cat1 from here: http://dellone2one.com/wp-content/uploads/2009/11/angry_wet_cat.jpg

and I downloaded cat2 from here: http://cvcl.mit.edu/hybrid/cat2.jpg

EDITED TO ADD THE FULL TRACEBACK WITHOUT THE HANDLING:
Here is the full traceback

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-11-22 12:37:56

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

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

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

这是另一条路线建设PIL。

My installation of PIL does the same. (PIL 1.1.7, Py 2.6, OSX 10.6)

EDIT:
Ah - the installation on a different OSX machine works. I know there are issues building PIL with JPG support on OSX, but I can't recall what the source of each of the two installations was, so I can't tell you how to fix it.

EDIT 2:
My best recollection is that building PIL with something like the instructions here yielded the working installation. The build and install commands had to be run sudo, and three invocations of gcc had to be manually modified to remove the "-arch ppc" option and re-run.

And here's another route to building PIL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文