Skip to content

Instantly share code, notes, and snippets.

@ConAlgorithm
Forked from Nannigalaxy/center_crop_scale.py
Created March 17, 2022 12:10
Show Gist options
  • Save ConAlgorithm/d9e60bf13957f58fcecd675c086c9fb7 to your computer and use it in GitHub Desktop.
Save ConAlgorithm/d9e60bf13957f58fcecd675c086c9fb7 to your computer and use it in GitHub Desktop.
Center Crop and Image Scaling functions in OpenCV using Python
# Center Crop and Image Scaling functions in OpenCV
# Date: 11/10/2020
# Written by Nandan M
import cv2
def center_crop(img, dim):
"""Returns center cropped image
Args:
img: image to be center cropped
dim: dimensions (width, height) to be cropped
"""
width, height = img.shape[1], img.shape[0]
# process crop width and height for max available dimension
crop_width = dim[0] if dim[0]<img.shape[1] else img.shape[1]
crop_height = dim[1] if dim[1]<img.shape[0] else img.shape[0]
mid_x, mid_y = int(width/2), int(height/2)
cw2, ch2 = int(crop_width/2), int(crop_height/2)
crop_img = img[mid_y-ch2:mid_y+ch2, mid_x-cw2:mid_x+cw2]
return crop_img
def scale_image(img, factor=1):
"""Returns resize image by scale factor.
This helps to retain resolution ratio while resizing.
Args:
img: image to be scaled
factor: scale factor to resize
"""
return cv2.resize(img,(int(img.shape[1]*factor), int(img.shape[0]*factor)))
if __name__ == "__main__":
image = cv2.imread('Kuvempu.jpg')
ccrop_img = center_crop(image, (500,400))
scale_img = scale_image(image, factor=1.5)
cv2.imwrite("Kuvempu_center_crop.jpg", ccrop_img)
cv2.imwrite("Kuvempu_scaled.jpg", scale_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment