Skip to content

Instantly share code, notes, and snippets.

@jkmackie
Created October 3, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkmackie/70ada8c662e3dd7a2b0da05ca3d05c1f to your computer and use it in GitHub Desktop.
Save jkmackie/70ada8c662e3dd7a2b0da05ca3d05c1f to your computer and use it in GitHub Desktop.
def crop_imag(imageFilePath: str, shakerange=20, shake_on=None):
'''Crop 1100x1100 images to 224X224. Optionally shake from center-crop
location (438,438,662,662). Return 224X224 image.'''
from PIL import Image
im = Image.open(imageFilePath)
rix = random.randint(-shakerange, shakerange) #get random int for x1, x2
riy = random.randint(-shakerange, shakerange) #get random int for y1, y2
if shake_on == True:
shaken_centercrop = (438+rix, 438+riy, 662+rix, 662+riy)
imCropped = im.crop(shaken_centercrop) #im.crop(x1, y1, x2, y2) - (x1,y1) top-left coord; (x2,y2) bottom-right coord
else:
centercrop = (438, 438, 662, 662)
imCropped = im.crop(centercrop)
return imCropped
def preprocess_3d_image(imPath: str):
'''Preprocess PIL format image for VGG16.'''
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
img = image.load_img(imPath) #load image into PIL format
arr = image.img_to_array(img) #convert to np.array
arr = preprocess_input(arr) #covert to BGR and zero-center color channels for VGG16
return image.array_to_img(arr) #return processed img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment