tags:

views:

263

answers:

3
from PIL import Image

im = Image.open(f)  #the size is 500x350
box = (0,0,100,100)
kay = im.crop(box)

It seems like there's nothing wrong with this, right?

That last line will result in an error and won't continue, but I don't know what the error is because it's AJAX and I can't debug ATM.

A: 

Try using integer coordinates instead of strings:

from PIL import Image

im = Image.open(f) #the size is 500x350 
box = (0,0,100,100) 
kay = im.crop(box)
Tom
It was original this, and it didn't work.
TIMEX
Strange, that works for me. Using strings gives a TypeError. Maybe you need to restart/reload your webserver for the change to take effect?
Tom
+1  A: 

If your controller is dealing with strings because the crop data is coming in via an ajax GET, it might be worth trying to make them into integers before applying the crop. Example from my terminal...

Trinity:~ kelvin$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> f = open("happy.jpg")
>>> im = Image.open(f)
>>> box = (0,0,100,100)
>>> kay = im.crop(box)
>>> kay
<PIL.Image._ImageCrop instance at 0xb1ea80>
>>> bad_box = ("0","0","100","100")
>>> nkay = im.crop(bad_box)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PIL/Image.py", line 742, in crop
    return _ImageCrop(self, box)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PIL/Image.py", line 1657, in __init__
    self.size = x1-x0, y1-y0
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>
wongo888
A: 

When you pick up the co-ordinates from the AJAX get request they are strings, you have to parse them to Int for the crop to succeed.

whatnick