1

I will explain myself better, the point is that I want to develop a code that displays an image to me, then with the mouse as the image is displayed I can select or crop it as I want.

So, for example, as this code does, it would select any section of the image:

from PIL import Image

im = Image.open("test.jpg")
crop_rectangle = (50, 50, 200, 200)
cropped_im = im.crop(crop_rectangle)

cropped_im.show()

That is basically it, all I want is to crop an image at the points or coordinates that I please, I have been searching but can not find any library that helps me to do so.

NOTE: The code I am showing is an answer on this post, in case you want to check it out: How can i select a part of a image using python?

EDIT: Here is something I foud that may help me in a first instance, but is not entirely what I am looking for, it at least lets me find the coordinates that I want from the image - with some modifications on the code - and then I will keep processing the image. By now it is not solving my issue but it is a beginning. --> Using "cv2.setMouseCallback" method

willyro93
  • 143
  • 1
  • 10
  • 1
    Have a read here... https://stackoverflow.com/a/42297587/2836621 – Mark Setchell Jul 08 '22 at 16:54
  • 1
    Check out the OpenCV Python tutorials. Specifically [GUI tutorials](https://docs.opencv.org/master/dc/d4d/tutorial_py_table_of_contents_gui.html), tutorial for [displaying image](https://docs.opencv.org/master/db/deb/tutorial_display_image.html), and tutorial for [mouse events](https://docs.opencv.org/master/db/d5b/tutorial_py_mouse_handling.html). – bfris Jul 08 '22 at 17:41

1 Answers1

1

The comments I received from Mark Setchell and bfris enlightened me, one with a C++ code and another one with a recommendation of OpenCV.

But in addition to their comments, I found this article where they explain exactly what I want to do, so I consider my question answered. Select ROI or Multiple ROIs [Bounding box] in OPENCV python.

import cv2
import numpy as np

#image_path
img_path="image.jpeg"

#read image
img_raw = cv2.imread(img_path)

#select ROI function
roi = cv2.selectROI(img_raw)

#print rectangle points of selected roi
print(roi)

#Crop selected roi from raw image
roi_cropped = img_raw[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]

#show cropped image
cv2.imshow("ROI", roi_cropped)

cv2.imwrite("crop.jpeg",roi_cropped)

#hold window
cv2.waitKey(0)

or

import cv2
import numpy as np

#image_path
img_path="image.jpeg"

#read image
img_raw = cv2.imread(img_path)

#select ROIs function
ROIs = cv2.selectROIs("Select Rois",img_raw)

#print rectangle points of selected roi
print(ROIs)

#Crop selected roi ffrom raw image

#counter to save image with different name
crop_number=0 

#loop over every bounding box save in array "ROIs"
for rect in ROIs:
    x1=rect[0]
    y1=rect[1]
    x2=rect[2]
    y2=rect[3]

        #crop roi from original image
    img_crop=img_raw[y1:y1+y2,x1:x1+x2]

        #show cropped image
        cv2.imshow("crop"+str(crop_number),img_crop)

    #save cropped image
    cv2.imwrite("crop"+str(crop_number)+".jpeg",img_crop)
        
    crop_number+=1

#hold window
cv2.waitKey(0)
willyro93
  • 143
  • 1
  • 10