Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 95a3bca

Browse files
authored
Merge pull request #264 from Pranjalmishra30/PranjalMishra
Cropping Images Dynamically
2 parents 6285713 + b6e8c14 commit 95a3bca

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Loading
Loading
Loading
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Image Cropper
2+
This code allows you to crop images and save them.
3+
4+
### Executing the code
5+
1. Run the .py file by running the command ``` python3 crop.py``` in the terminal or cmd.
6+
2. Use the **Left** mouse button to drag out a rectangular region of the image you want to crop. **Release** the button, once you are done.
7+
3. The selected rectangular is shown on the image.
8+
4. Press **c** to crop the image. A new window opens up.
9+
a) Press **s** to save the cropped image.
10+
b) Press **r** to reset and return to the original image.
11+
5. Repeat from step 2 to crop more images.
12+
13+
### Demo
14+
![](https://github.com/Pranjalmishra30/Awesome-Python-Scripts/blob/PranjalMishra/Image-Processing/ImageCropper/Data/CropDEMO.gif)
15+
16+
### Refrences
17+
1. Mouse events [tutorial](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_mouse_handling/py_mouse_handling.html)
18+
2. Pyimagesearch [tutorial](https://www.pyimagesearch.com/2015/03/09/capturing-mouse-click-events-with-python-and-opencv/)

Image-Processing/ImageCropper/crop.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import cv2
2+
refpt = [] #List of refrence points
3+
4+
def select_roi (event,x,y,flags,param):
5+
global refpt #Global refrences
6+
7+
if event == cv2.EVENT_LBUTTONDOWN: # When the left mouse button is cliked
8+
refpt = [(x , y)]
9+
10+
elif event == cv2.EVENT_LBUTTONUP: # When the left mouse button is released
11+
refpt.append((x , y)) # recording the last coordinates
12+
cv2.rectangle(img_main,refpt[0],refpt[1],(0,255,0),2)
13+
cv2.imshow("frame",img_main)
14+
print("Selection Successful")
15+
16+
img = cv2.imread("Data/Man_United.jpeg")
17+
img_main = cv2.resize(img,(400,400)) #Resizing image
18+
19+
clone = img_main.copy() # To reset the image after cropping
20+
clone2 = img_main.copy() # To crop a section out without affecting the original image
21+
22+
cv2.namedWindow("frame")
23+
cv2.setMouseCallback("frame",select_roi)
24+
25+
i = 1 # Numbering for saving images
26+
27+
while True:
28+
cv2.imshow("frame",img_main)
29+
var = cv2.waitKey(0)
30+
31+
# Select a region , then press c to crop that portion
32+
33+
if var == ord('c'): # Crop selected images
34+
35+
if len(refpt) == 2:
36+
roi = clone2[ refpt[0][1] : refpt[1][1] , refpt[0][0] : refpt[1][0] ] # [x1:x2 , y1:y2]
37+
cv2.namedWindow("Crop")
38+
cv2.imshow("Crop",roi)
39+
print("Cropped")
40+
41+
var2 = cv2.waitKey(0)
42+
43+
if var2 == ord('s'): # Saving cropped image
44+
cv2.imwrite("Data/cropped image{}.png".format(i),roi)
45+
i = i+1
46+
print("image saved\n")
47+
cv2.destroyWindow("Crop")
48+
img_main = clone.copy()
49+
50+
elif var2 == ord('r'): # Reset
51+
cv2.destroyWindow("Crop")
52+
print("Reset\n")
53+
img_main = clone.copy()
54+
55+
elif var == ord('q'): # Exit the loop
56+
print("Exiting ...")
57+
break
58+
59+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)