简体   繁体   English

如何解决这个错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop'

[英]How to solve this error: AttributeError: 'numpy.ndarray' object has no attribute 'crop'

I want to crop images with different sizes to get the same size to futher process them.我想裁剪不同尺寸的图像以获得相同的尺寸以进一步处理它们。 I wrote the following code:我写了以下代码:

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

This outputs me the following error: AttributeError: 'numpy.ndarray' object has no attribute 'crop'这会输出以下错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop'

I am coding beginner and I dont know what I have to do.我是编码初学者,我不知道我必须做什么。

It's because numpy doesn't have crop functionality.这是因为 numpy 没有裁剪功能。 Try opening the image using PIL library and use the crop function as follows:尝试使用 PIL 库打开图像并使用crop function 如下:

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

And then proceed with the crop.然后继续进行裁剪。 Or Alternatively, you can crop it yourself without the function as follows:或者,您可以在没有 function 的情况下自行裁剪,如下所示:

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

I want to crop images with different sizes to get the same size to futher process them.我想裁剪不同尺寸的图像以获得相同的尺寸以进一步处理它们。 I wrote the following code:我写了以下代码:

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

This outputs me the following error: AttributeError: 'numpy.ndarray' object has no attribute 'crop'这向我输出以下错误: AttributeError: 'numpy.ndarray' object has no attribute 'crop'

I am coding beginner and I dont know what I have to do.我是编码初学者,我不知道我必须做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“ numpy.ndarray”对象没有属性“ A” - AttributeError: 'numpy.ndarray' object has no attribute 'A' AttributeError: 'numpy.ndarray' object has no attribute 'score' 错误 - AttributeError: 'numpy.ndarray' object has no attribute 'score' error AttributeError: 'numpy.ndarray' object 没有属性 'append' 错误 - AttributeError: 'numpy.ndarray' object has no attribute 'append' error 使用 image.crop() 模糊图像 - AttributeError: 'numpy.ndarray' object 没有属性 'crop' - blurring an image with image.crop() - AttributeError: 'numpy.ndarray' object has no attribute 'crop' 如何解决“AttributeError:'numpy.ndarray'对象没有属性'lower'”? - How solved "AttributeError: 'numpy.ndarray' object has no attribute 'lower'"? 如何修复此错误:AttributeError: 'numpy.ndarray' object has no attribute 'apply' - How to fix this error :AttributeError: 'numpy.ndarray' object has no attribute 'apply' 我如何通过消除错误来训练管道中的GaussianNB [AttributeError:'numpy.ndarray'对象没有属性'lower'] - How can i train GaussianNB in pipeline by removing error[AttributeError: 'numpy.ndarray' object has no attribute 'lower'] Numpy和Matplotlib-AttributeError:“ numpy.ndarray”对象没有属性“ replace” - Numpy and Matplotlib - AttributeError: 'numpy.ndarray' object has no attribute 'replace' 计算热指数时如何解决 Metpy ('numpy.ndarray' object has no attribute 'to') 错误? - How to solve Metpy ('numpy.ndarray' object has no attribute 'to') error when calculating heat index? 如何修复此错误:numpy.ndarray“对象没有属性” append” - how to fix this error : numpy.ndarray “ object has no attribute ”append"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM