Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:crop()接受1到2个位置参数,但给出了5个_Python_Image_Python Imaging Library_Crop - Fatal编程技术网

Python TypeError:crop()接受1到2个位置参数,但给出了5个

Python TypeError:crop()接受1到2个位置参数,但给出了5个,python,image,python-imaging-library,crop,Python,Image,Python Imaging Library,Crop,但当我尝试裁剪图像时,如下所示: from PIL import Image img=Image.open('/home/ahmed/internship/cnn_ocr/image1.png') img.size (2458, 3504) 我得到了以下错误: img.crop(414,122,650,338) 回溯(最近一次呼叫最后一次): 文件“/usr/lib/python3.5/code.py”,第91行,运行代码 exec(代码,self.locals) 文件“”,第1行,在 Ty

但当我尝试裁剪图像时,如下所示:

from PIL import Image
img=Image.open('/home/ahmed/internship/cnn_ocr/image1.png')
img.size
(2458, 3504)
我得到了以下错误:

img.crop(414,122,650,338)
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.5/code.py”,第91行,运行代码
exec(代码,self.locals)
文件“”,第1行,在
TypeError:crop()接受1到2个位置参数,但给出了5个

但是
crop()
接受4个参数:左、上、右、下。出了什么问题

No
crop
采用了一个显式参数:一个4元组(当然也隐式使用了
self
)。各国:

Image.crop(box=None)

从该图像返回一个矩形区域。
框是一个4元组
定义左、上、右和下像素坐标

注意:在枕头3.4.0之前,这是一个懒惰的操作

参数:
box
-裁剪矩形,作为(左、上、右、下)元组。
返回类型:图像
返回:图像对象

(已添加格式)

因此,您应该将其改写为:

Traceback (most recent call last):
  File "/usr/lib/python3.5/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: crop() takes from 1 to 2 positional arguments but 5 were given

谢谢,这很有效。但是在制作img.crop((414122650338))之后它不会裁剪图像,我尝试了img.size它给了我图像的原始大小,然后使用img.save('cropped_image.png')我得到了原始图像image@vincent:请参阅下面的注释:裁剪无法内联工作:您需要指定结果(也许您可以将其设置为
img
,因此
img=img.crop(..)
)。
img.crop((414,122,650,338))
#        ^    4-tuple    ^
some_other_img = img.crop((414,122,650,338))