【问题标题】:How to solve this error: AttributeError: 'numpy.ndarray' object has no attribute 'crop'如何解决此错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop'
【发布时间】:2021-05-16 00:20:16
【问题描述】:

我想裁剪不同尺寸的图像以获得相同的尺寸以进一步处理它们。我写了以下代码:

import glob
import cv2
import os
from matplotlib import pyplot as plt


inputFolder = "C:\\Users\\die5k\\Desktop\\hist\\Cropping\\input"
storeDirectory =  "C:\\Users\\die5k\\Desktop\\hist\\Cropping\\output"

path = glob.glob(inputFolder + "\\*.png")
cv_img = []
image_no = 1
for img in path:
    n = cv2.imread(img)
    cv_img.append(n)
    print(img)

    os.chdir(storeDirectory)
    cropped_img = n.crop(((w-100)//2, (h-100)//2, (w+100)//2, (h+100)//2))


    filename = "Figure_" + str(image_no) + ".png"

    plt.gcf().savefig(filename)
   

    print(image_no)
    image_no += 1

这会输出以下错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop'

我是编码初学者,我不知道我必须做什么。

【问题讨论】:

    标签: python image opencv crop


    【解决方案1】:

    这是因为 numpy 没有裁剪功能。尝试使用 PIL 库打开图像并使用crop 函数,如下所示:

    from PIL import Image    
    n = Image.open(path)
    

    然后继续进行裁剪。 或者,您也可以不使用以下功能自行裁剪:

    cropped_img = n[((h-100)//2):((h-100)//2)+((h+100)//2), ((w-100)//2):((w-100)//2)+((w+100)//2)]
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2022-12-04
      • 2022-11-11
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多