PIL's thumbnail function returns NoneType

  • Thread starter Mirat Can Bayrak
  • Start date
M

Mirat Can Bayrak

Can you try it? it is about me or it is a bug?

In [1]: import Image

In [2]: im = Image.open("r.png")

In [3]: type(im)
Out[3]: <type 'instance'>

In [4]: thm = im.thumbnail((200,200))

In [5]: type(thm)
Out[5]: <type 'NoneType'>
 
H

Hrvoje Niksic

Mirat Can Bayrak said:
Can you try it? it is about me or it is a bug?

Neither. im.thumbnail() modifies the existing image object by
converting it to a thumbnail. In Python such methods by convention
return None.

The documentation explicitly mentions that:

Also note that this function modifies the Image object in
place. If you need to use the full resolution image as well, apply
this method to a copy of the original image. This method returns
None.
In [4]: thm = im.thumbnail((200,200))

In [5]: type(thm)
Out[5]: <type 'NoneType'>

Instead of thm, simply keep using ihm.
 
T

Terry Reedy

Hrvoje said:
Neither. im.thumbnail() modifies the existing image object by
converting it to a thumbnail. In Python such methods by convention
return None.

The documentation explicitly mentions that:

Also note that this function modifies the Image object in
place. If you need to use the full resolution image as well, apply
this method to a copy of the original image. This method returns
None.

Indeed, the purpose of returning None is to remind that the method does
mutation in place.

In [4]: thm = im.thumbnail((200,200))

In [5]: type(thm)
Out[5]: <type 'NoneType'>

Instead of thm, simply keep using ihm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top