在裁剪图像时,我会遇到一些错误,比如“没有足够的值来解包”

2024-10-01 09:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试裁剪图像并将其打印到matplotlib.pyplot中,但显示了一个错误

import matplotlib.pyplot as plt
import numpy as np
import cv2
image = cv2.imread("tt.jpg")
type(image)
print(image.shape)
W = 100
H = 100
def crop_center(img , cropx , cropy):
    y,x,c = image.shape
    startx = x//2-(cropx//2)
    starty = y//2-(cropy//2)
    return img[starty:starty+cropy , startx:startx+cropx]
croped = crop_center(image,W,H)
print(croped)
print(plt.imshow(croped, cmap='gray'))
plt.show() # with scaled size

**错误:**

  Traceback (most recent call last):
  File "openimage.py", line 96, in <module>
  croped = crop_center(scaled,W,H)
  File "openimage.py", line 92, in crop_center
  y,x,c = image.shape
  ValueError: not enough values to unpack (expected 3, got 2)

Tags: cropimageimportmatplotlib错误pltcenterprint