TypeError when resizing an image using PIL in Python

Note. This is a question with answers to a question.

I am trying to resize an image using Python code, but I am 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 that I use:

 import Image logo = Image.open("my_image.png") logo = logo.resize((100, 100), Image.ANTIALIAS) logo.save("my_image_resized.png") 
+4
source share
2 answers

After some research, I found fooobar.com/questions/1489526 / ... which does not match, but seems to be related.

@SaranshMohapatra said he had PIL and Pillow installed (just like me), and he solved the problem of removing one of them. But I solved the problem differently.

I just changed the import of Image .

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

So, the final excerpt is as follows:

 from PIL import Image logo = Image.open("my_image.png") logo = logo.resize((100, 100), Image.ANTIALIAS) logo.save("my_image_resized.png") 
+5
source

The following code works for me and seems portable:

 try: from PIL import Image from PIL import ImageDraw except ImportError: import Image import ImageDraw 
0
source

Source: https://habr.com/ru/post/1489524/


All Articles