注意:这是一个自行回答的问题。
我正在尝试使用Python代码调整图像大小但是我遇到了以下奇怪的错误:
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
我正在使用的代码是:
import Image
logo = Image.open("my_image.png")
logo = logo.resize((100, 100), Image.ANTIALIAS)
logo.save("my_image_resized.png")
答案 0 :(得分:5)
经过一些研究后,我发现this question on StackOverflow不一样,但似乎是相关的。
我刚刚更改了Image
导入。
从import Image
到:from PIL import Image
,解决了这个问题!
所以最终剪辑看起来像这样:
from PIL import Image
logo = Image.open("my_image.png")
logo = logo.resize((100, 100), Image.ANTIALIAS)
logo.save("my_image_resized.png")
答案 1 :(得分:0)
对我来说,并且似乎是可移植的,以下代码:
try:
from PIL import Image
from PIL import ImageDraw
except ImportError:
import Image
import ImageDraw