from PIL import Image
im = Image.open(f) #the size is 500x350
box = (0,0,100,100)
kay = im.crop(box)
这好像没什么问题吧?
最后一行会导致错误,不会继续,但我不知道错误是什么,因为它是 AJAX,我无法调试 ATM。
最佳答案
如果您的 Controller 正在处理字符串,因为裁剪数据是通过 ajax GET 传入的,那么在应用裁剪之前尝试将它们变成整数可能是值得的。来 self 的终端的示例...
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'
>>>
关于python - 为什么我不能在 Python PIL 中裁剪此图像? (简单的语法问题?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1483393/