Python - 枕头缩略图创建错误

时间:2017-03-22 01:57:10

标签: python pillow

当我尝试从任何图像创建缩略图时,发生了以下错误:

TypeError: 'int' object has no attribute '__getitem__'

命令:

./manage.py shell
from PIL import Image
i = Image.open('1.jpg')
i.thumbnail(200)

错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-3e0cd0330e45> in <module>()
----> 1 i.thumbnail(200)

/home/user/.virtualenvs/project/lib/python2.7/site-packages/PIL/Image.pyc
in thumbnail(self, size, resample)
   1828         # preserve aspect ratio
   1829         x, y = self.size
-> 1830         if x > size[0]:
   1831             y = int(max(y * size[0] / x, 1))
   1832             x = int(size[0])

TypeError: 'int' object has no attribute '__getitem__'

某处我读到了一些关于libjpeg包的内容,但是我收到了所有类型文件(gif,png,...)的错误。

我使用archlinux:

$ uname -a
Linux chalist 4.9.11-1-ARCH # 1 SMP PREEMPT 
Sun Feb 19 13:45:52 UTC 2017 x86_64 GNU/Linux

1 个答案:

答案 0 :(得分:1)

大小必须采用元组的形式,例如(200, 200)。所以你的新代码将是:

from PIL import Image
i = Image.open('1.jpg')
i.thumbnail((200, 200))

以下是参考网页的链接:http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#create-thumbnails