Created
October 3, 2022 16:52
-
-
Save jkmackie/70ada8c662e3dd7a2b0da05ca3d05c1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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