0

I have a trouble which didn't have knowledge how to crop all of the images with specific crop size setting and need loop it to all the images within the folder. once done, let it export to new folder or maybe can set a custom directory path.

I have tried some of the example which i found but it's single file read, only can preview the cropped image (not save) and need manually edit the path.

import cv2
y=0
x=0
h=1000
w=1356
crop = image[y:y+h, x:x+w]
cv2.imshow('Image', crop)
cv2.waitKey(0) 

I not sure is there any code could be reference and be used.

Neko Lim
  • 53
  • 5
  • Hello I think this will answer your question https://stackoverflow.com/questions/68767648/how-to-crop-all-pictures-in-a-folder-and-save-it-to-another-folder-by-python – Rashid88 Jan 13 '23 at 03:22
  • Im not sure about this "image_cropped_top = raw_image.crop((2125, 70, 2148, 310))" value settings, more preferred with x,y axis and height,width value. – Neko Lim Jan 13 '23 at 03:34

1 Answers1

0

Try this will work on OpenCV

import cv2
import numpy as np

img = cv2.imread('test.jpg')
print(img.shape) # Print image shape
cv2.imshow("original", img)

cropped_image = img[80:280, 150:330

cv2.imshow("cropped", cropped_image)

cv2.imwrite("Cropped Image.jpg", cropped_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

the above will solve your read only problem

import cv2
import numpy as np

img = cv2.imread('test.jpg')
path= 'D:/OpenCV/Scripts/Images'
print(img.shape) # Print image shape
cv2.imshow("original", img)

cropped_image = img[80:280, 150:330

cv2.imshow("cropped", cropped_image)

cv2.imwrite('Path/Image.jpg', image_name)

cv2.waitKey(0)
cv2.destroyAllWindows()

using the above can help you save image in youre desired folder you can set your path in "path=xxx"

Rashid88
  • 34
  • 5
  • Hi, i just try your code and I got a syntax error on the cv2.imshow("cropped", cropped_image) – Neko Lim Jan 13 '23 at 06:19
  • here you go one more reference https://stackoverflow.com/questions/41586429/opencv-saving-images-to-a-particular-folder-of-choice – Rashid88 Jan 13 '23 at 06:30