tags:

views:

426

answers:

3

Python beginner question. Code below should explain my problem:

import Image

resolution = (200,500)
scaler = "Image.ANTIALIAS"

im = Image.open("/home/user/Photos/DSC00320.JPG")

im.resize(resolution , scaler)

RESULT:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1255, in resize
    raise ValueError("unknown resampling filter")
ValueError: unknown resampling filter

This one works:

im.resize(resolution , Image.ANTIALIAS)
+1  A: 

As you said, im.resize(resolution , Image.ANTIALIAS) is the solution

You have to take care than this is different than im.resize(resolution , "Image.ANTIALIAS").

In your example, the variable scaler has the string "Image.ANTIALIAS" as a value, that is different than the value Image.ANTIALIAS.

A string representing xxxx is different than the value xxxx, exactly as the string "12" is completely different than the integer 12.

ThibThib
+4  A: 

Well, then Image.ANTIALIAS is not a string, so don't treat it as one:

scaler = Image.ANTIALIAS
unwind
+3  A: 

As @ThibThib said using "Image.ANTIALIAS" is not the same thing as Image.ANTIALIAS. But if you always expect to get the resample value as a string you could do the following:

scaler = 'ANTIALIAS'
resample = {
    'ANTIALIAS': Image.ANTIALIAS,
    'BILINEAR': Image.BILINEAR,
    'BICUBIC': Image.BICUBIC
}

im.resize(resolution , resample[scaler])
Nadia Alramli
Or, perhaps:`im.resize(resolution, getattr(Image, scaler))`
ΤΖΩΤΖΙΟΥ