Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Image-Processing/ImageCropper/Data/CropDEMO.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Image-Processing/ImageCropper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Image Cropper
This code allows you to crop images and save them.

### Executing the code
1. Run the .py file by running the command ``` python3 crop.py``` in the terminal or cmd.
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.
3. The selected rectangular is shown on the image.
4. Press **c** to crop the image. A new window opens up.
a) Press **s** to save the cropped image.
b) Press **r** to reset and return to the original image.
5. Repeat from step 2 to crop more images.

### Demo
![](https://github.com/Pranjalmishra30/Awesome-Python-Scripts/blob/PranjalMishra/Image-Processing/ImageCropper/Data/CropDEMO.gif)

### Refrences
1. Mouse events [tutorial](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_mouse_handling/py_mouse_handling.html)
2. Pyimagesearch [tutorial](https://www.pyimagesearch.com/2015/03/09/capturing-mouse-click-events-with-python-and-opencv/)
59 changes: 59 additions & 0 deletions Image-Processing/ImageCropper/crop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import cv2
refpt = [] #List of refrence points

def select_roi (event,x,y,flags,param):
global refpt #Global refrences

if event == cv2.EVENT_LBUTTONDOWN: # When the left mouse button is cliked
refpt = [(x , y)]

elif event == cv2.EVENT_LBUTTONUP: # When the left mouse button is released
refpt.append((x , y)) # recording the last coordinates
cv2.rectangle(img_main,refpt[0],refpt[1],(0,255,0),2)
cv2.imshow("frame",img_main)
print("Selection Successful")

img = cv2.imread("Data/Man_United.jpeg")
img_main = cv2.resize(img,(400,400)) #Resizing image

clone = img_main.copy() # To reset the image after cropping
clone2 = img_main.copy() # To crop a section out without affecting the original image

cv2.namedWindow("frame")
cv2.setMouseCallback("frame",select_roi)

i = 1 # Numbering for saving images

while True:
cv2.imshow("frame",img_main)
var = cv2.waitKey(0)

# Select a region , then press c to crop that portion

if var == ord('c'): # Crop selected images

if len(refpt) == 2:
roi = clone2[ refpt[0][1] : refpt[1][1] , refpt[0][0] : refpt[1][0] ] # [x1:x2 , y1:y2]
cv2.namedWindow("Crop")
cv2.imshow("Crop",roi)
print("Cropped")

var2 = cv2.waitKey(0)

if var2 == ord('s'): # Saving cropped image
cv2.imwrite("Data/cropped image{}.png".format(i),roi)
i = i+1
print("image saved\n")
cv2.destroyWindow("Crop")
img_main = clone.copy()

elif var2 == ord('r'): # Reset
cv2.destroyWindow("Crop")
print("Reset\n")
img_main = clone.copy()

elif var == ord('q'): # Exit the loop
print("Exiting ...")
break

cv2.destroyAllWindows()