1
from PIL import Image

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

这个好像没什么问题吧?

最后一行将导致错误并且不会继续,但我不知道错误是什么,因为它是 AJAX,我无法调试 ATM。

4

3 回答 3

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'
>>> 
于 2009-09-27T13:02:59.407 回答
1

当您从 AJAX 获取请求中获取坐标时,它们是字符串,您必须将它们解析为 Int 才能成功进行裁剪。

于 2009-09-27T14:27:47.510 回答
0

尝试使用整数坐标而不是字符串:

from PIL import Image

im = Image.open(f) #the size is 500x350 
box = (0,0,100,100) 
kay = im.crop(box)
于 2009-09-27T11:39:50.730 回答