Python (image library): Resample string as argument

Python beginner question. The 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

      

It works:

im.resize(resolution , Image.ANTIALIAS)

      

+2


source to share


3 answers


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



scaler = Image.ANTIALIAS

      

+7


source


As @ThibThib said with "Image.ANTIALIAS" this is not the same as Image.ANTIALIAS. But if you always expect to get the resample value as a string, you can do the following:



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

im.resize(resolution , resample[scaler])

      

+3


source


How do you say im.resize(resolution , Image.ANTIALIAS)

- the decision

You have to care how it differs from im.resize(resolution , "Image.ANTIALIAS")

.

In your example, the variable scaler

has a string "Image.ANTIALIAS"

as a value that is different from the value Image.ANTIALIAS

.

The string representing xxxx is different than the xxxx value, just like a string is "12"

completely different from integers 12

.

+2


source







All Articles