Skip to content

Instantly share code, notes, and snippets.

@ArnoutDevos
Created January 11, 2018 13:59
Show Gist options
  • Save ArnoutDevos/3d325a0b442251b6648f7983259e90ee to your computer and use it in GitHub Desktop.
Save ArnoutDevos/3d325a0b442251b6648f7983259e90ee to your computer and use it in GitHub Desktop.
import cv2
def imgread(path):
print "Image:", path.split("/")[-1]
# Read in the image using python opencv
img = cv2.imread(path)
img = img / 255.0
print "Raw Image Shape: ", img.shape
# Center crop the image
short_edge = min(img.shape[:2])
W, H, C = img.shape
to_crop = min(W, H)
cent_w = int((img.shape[1] - short_edge) / 2)
cent_h = int((img.shape[0] - short_edge) / 2)
img_cropped = img[cent_h:cent_h+to_crop, cent_w:cent_w+to_crop]
print "Cropped Image Shape: ", img_cropped.shape
# Resize the cropped image to 224 by 224 for VGG16 network
img_resized = cv2.resize(img_cropped, (224, 224), interpolation=cv2.INTER_LINEAR)
print "Resized Image Shape: ", img_resized.shape
return img_resized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment