Skip to content

Instantly share code, notes, and snippets.

@samyumobi
Created November 13, 2021 12:51
Show Gist options
  • Select an option

  • Save samyumobi/1e7f8b8c8b82fe093fa5cad3b4ae91fc to your computer and use it in GitHub Desktop.

Select an option

Save samyumobi/1e7f8b8c8b82fe093fa5cad3b4ae91fc to your computer and use it in GitHub Desktop.
import sys, cv2
import matplotlib.pyplot as plt
## Load the image
f = sys.argv[1]
img = cv2.imread('/a.jpeg')
## display the image
plt.imshow(img)
plt.show()
## Cropping an image
h, w = img.shape[:2]
sr, er = int(0.21*h), int(0.73*h)
sc, ec = int(0.37*w), int(0.92*w)
## crop image using numpy
img_cropped = img[sr:er, sc:ec]
plt.imshow(img_cropped)
plt.show()
## Resize image to 1.3 times original size
## Scale the image unifiormly
# scaling_factor = 1.3
# img_scaled = cv2.resize(img, None,fx = scaling_factor )
# plt.imshow(img_scaled)
# plt.show()
## save the cropped image to an output file
output_file = f[:-4] + '_cropped.jpg'
cv2.imwrite(output_file,img_cropped)
## display images until key is pressed
cv2.waitKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment