Skip to content

Instantly share code, notes, and snippets.

@Nannigalaxy
Last active May 17, 2022 09:21
Show Gist options
  • Save Nannigalaxy/35dd1d0722f29672e68b700bc5d44767 to your computer and use it in GitHub Desktop.
Save Nannigalaxy/35dd1d0722f29672e68b700bc5d44767 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)
@teddymaxx
Copy link

how i can modify it for multiple pictures in a folder ?

@Nannigalaxy
Copy link
Author

use the following code inside the above script.

import os

# source images path to be cropped
PATH = "path/to/images"
img_list = os.listdir(PATH)

# create output folder to save new cropped images 
os.mkdir("output")

for img_f in img_list:
    if img_f.endswith("jpg"):
      img = cv2.imread(os.path.join(path,img_f))
      cropped_img = center_crop(img, (500,400))
      cv2.imwrite("output/image_center_crop.jpg", cropped_img)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment