python - 为什么我不能在 Python PIL 中裁剪此图像? (简单的语法问题?)

标签 python image python-imaging-library

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/

相关文章:

python - 如何获取 PIL 图像作为 Base64 编码的字符串

python - 如何检查列表中的数据类型,无论列表有多深

c# - 如何从 C# 过渡到 Python?

css - Bootstrap 4 带图像背景的垂直图像菜单

ios - 图片不适合 WebView

python - Pillow 无法正确处理 PNG 文件

python - scipy.optimize()值错误:Shape mismatch for sum

python - 如何使用Python中的Tkinter,使用几个选定颜色的迭代来更改Python中的圆圈颜色,这些颜色随着每次按下按钮而变化?

java - 将图像添加到 GUI

python - 在 Python 中合并图像