Q.68
Explain the process of Image Resizing.
In this, firstly, we will write a function for resizing the image. Here, we will resize the image to 227x227. The function resize can be defined as:
def resize(img, input_height, input_width):
Now, we obtain the aspect ratio of the image by dividing the width by the height.
original_aspect = img.shape[1]/float(img.shape[0])
However, if the aspect ratio is greater than 1, it indicates that the image is wide, that to say it is in the landscape mode. For adjusting the image height and return the resized image use the following code:
if(original_aspect>1):
new_height = int(original_aspect * input_height)
return skimage.transform.resize(img, (input_width,
new_height), mode='constant', anti_aliasing=True, anti_aliasing_sigma=None)
Further, if the aspect ratio is less than 1, it indicates the portrait mode. For adjusting the width use the following code:
if(original_aspect<1):
new_width = int(input_width/original_aspect)
return skimage.transform.resize(img, (new_width,
input_height), mode='constant', anti_aliasing=True, anti_aliasing_sigma=None)
Lastly, if the aspect ratio equals 1, we do not make any height/width adjustments.
if(original_aspect == 1):
return skimage.transform.resize(img, (input_width,
input_height), mode='constant', anti_aliasing=True, anti_aliasing_sigma=None)