Reputation: 59
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'
I am coding beginner and I dont know what I have to do.
Upvotes: 1
Views: 9275
Reputation: 1458
It's because numpy doesn't have crop functionality. Try opening the image using PIL library and use the crop
function as follows:
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:
cropped_img = n[((h-100)//2):((h-100)//2)+((h+100)//2), ((w-100)//2):((w-100)//2)+((w+100)//2)]
Upvotes: 2