1

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.

1 Answer 1

2

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)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.