在Python中使用PIL调整图像大小时的TypeError [英] TypeError when resizing an image with PIL in Python

查看:488
本文介绍了在Python中使用PIL调整图像大小时的TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是一个自行回答的问题。



我正在尝试使用Python代码调整图像大小,但我是得到以下奇怪错误:

 回溯(最近一次调用最后一次):
文件resize.py,行5,在< module>中
logo.save(StartMyProjects_resized.png,format =PNG)
文件/usr/lib/python2.7/dist-packages/PIL/Image.py,第1439行,保存
save_handler(self,fp,filename)
文件/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py,第572行,在_save
ImageFile中。 _save(im,_idat(fp,chunk),[(zip,(0,0)+ im.size,0,rawmode)])
File/usr/lib/python2.7/dist- packages / PIL / ImageFile.py,第481行,在_save
e = Image._getencoder(im.mode,e,a,im.encoderconfig)
File/usr/lib/python2.7/ dist-packages / PIL / Image.py,第399行,_getencoder
返回apply(编码器,(模式)+ args + extra)
TypeError:需要一个整数

我正在使用的代码是:

  import Image 

logo = Image.open(my_image.png)
logo = logo.resize((100,100),Image.ANTIALIAS)
logo.save(my_image_resized.png)


解决方案

AFTE我在一些研究中发现这个问题在StackOverflow上不一样但似乎@ b
$ b

@SaranshMohapatra说他安装了PIL和Pillow(和我一样),他解决了卸载其中一个的问题。但我以不同的方式解决了这个问题。



我刚刚更改了图像导入。



从:导入图像到:来自PIL导入图像并修复了问题!



所以最终剪辑看起来像这样:

 来自PIL导入图片

logo = Image.open(my_image.png)
logo = logo.resize((100,100),Image.ANTIALIAS)
logo.save (my_image_resized.png)


Note: This is a self-answered question.

I'm trying to resize an image with Python code but I'm getting the following strange error:

Traceback (most recent call last):
  File "resize.py", line 5, in <module>
    logo.save("StartMyProjects_resized.png", format="PNG")
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 572, in _save
    ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])
  File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 481, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 399, in _getencoder
    return apply(encoder, (mode,) + args + extra)
TypeError: an integer is required

And the code I'm using is:

import Image

logo = Image.open("my_image.png")
logo = logo.resize((100, 100), Image.ANTIALIAS)
logo.save("my_image_resized.png")

解决方案

After some research I found this question on StackOverflow which is not the same but seems to be related.

@SaranshMohapatra said that he had both PIL and Pillow installed (the same as me) and he solved the problem uninstalling one of them. But I solved the problem in a different way.

I just changed the Image import.

From: import Image to: from PIL import Image and that fixed the problem!

So the final snipped looks like this:

from PIL import Image

logo = Image.open("my_image.png")
logo = logo.resize((100, 100), Image.ANTIALIAS)
logo.save("my_image_resized.png")

这篇关于在Python中使用PIL调整图像大小时的TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆