TypeError: crop() takes from 1 to 2 positional arguments but 5 were given

TypeError: crop() takes from 1 to 2 positional arguments but 5 were given

from PIL import Image
img=Image.open('/home/ahmed/internship/cnn_ocr/image1.png')
img.size
(2458, 3504)

但是当我尝试如下裁剪图像时:

img.crop(414,122,650,338)

我收到以下错误:

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

但是crop()需要4个参数:left, top, right, bottom。怎么了

没有crop 接受一个显式参数:一个四元组(当然还有隐含的self). documentation 状态:

Image.crop(box=None)

Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

Note: Prior to Pillow 3.4.0, this was a lazy operation.

Parameters:
   box - The crop rectangle, as a (left, upper, right, lower)-tuple.
Return type: Image
Returns: An Image object.

(已添加格式)

所以你应该将它重写为:

img.crop(<b>(</b>414,122,650,338<b>)</b>)
#        ^    4-tuple    ^

此外,你最好将输出分配给一个变量(可能是img本身):

<b>some_other_img = </b>img.crop((414,122,650,338))

我也遇到了同样的错误

x = 0
y = 0

w = 1000/3
h = 667/10

mac.crop(x,y,w,h)

类型错误:crop() 接受 1 到 2 个位置参数,但给出了 5 个

之后,我尝试了下面的代码得到一个输出

mac.crop((x,y,w,h))

确保您再次尝试,下面提到的代码以获得明确的结果。

img.crop((414,122,650,338))