[英]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.