from PIL import Image
im = Image.open(f) #the size is 500x350
box = (0,0,100,100)
kay = im.crop(box)
这似乎没有错,对吗?
最后一行会导致错误并且不会继续,但我不知道错误是什么,因为它是AJAX而我无法调试ATM。
答案 0 :(得分:4)
如果您的控制器正在处理字符串,因为裁剪数据是通过ajax GET进入的,那么在应用裁剪之前尝试将它们变成整数可能是值得的。来自我的终端的例子......
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'
>>>
答案 1 :(得分:1)
当您从AJAX get请求中获取坐标时,它们是字符串,您必须将它们解析为Int才能使裁剪成功。
答案 2 :(得分:0)
尝试使用整数坐标而不是字符串:
from PIL import Image
im = Image.open(f) #the size is 500x350
box = (0,0,100,100)
kay = im.crop(box)