![]() The first parameter takes the file of the image variable, and the second parameter takes the dimension variable. ![]() The “ cv2.resize()” function takes three parameter values.The width and height dimensions will be saved in a variable named “ dimension”. ![]() The “ image.shape” is used for width, and “ image.shape” is used for height. The image scale number is divided by “100” and multiplied by the height and width of the original image.To downscale the image, we input “ 30”, which is stored in the variable “ image_scale”.100% means that the value has its original dimension. The variable “ image_scale” is used to store the percentage value of the image size.The parameter “ pic.shape” gets the original image’s dimensions.The flags specify how the function reads the image, such as in grayscale or color. The function named “cv2.imread()” takes two parameter values, the first parameter takes the path of the image and the second parameter takes the flags of the image.The Python library named “ cv2” is imported.Print('Resized Dimensions of our image is : ',resized_img.shape) Resized_img = cv2.resize(pic, dimension, interpolation = cv2.INTER_AREA) Height_img = int(pic.shape * image_scale / 100) Width_img = int(pic.shape * image_scale / 100) Print('Original Dimensions of our Image is : ',pic.shape) 04 step # What Size do You Want to Convert the Image to? size = (200,200) sh, sw = size print(sh, sw) 200 200Ġ5 step # Choose an OpenCV Interpolation Method # interpolation method if h > sh or w > sw: # shrinking image interp = cv2.INTER_AREA else: # stretching image interp = cv2.INTER_CUBICĠ6 step # Calculate the Pads in case the Images are Horizontal or Vertical # aspect ratio of image aspect = w /h print(f"The original aspect ratio is ") print("With this math we proportionally convert the image to 200 x 200") The original aspect ratio is 1.33 h=150, new_w=200 The new aspect ratio is still 1.33 With this math we proportionally convert the image to 200 x 200Ġ7 step # Mount the Image with Pads, if necessary scaled_img = cv2.resize(img, (new_w, new_h), interpolation =interp) scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType =cv2.BORDER_CONSTANT, value =127) i = plt.imshow(scaled_img, cmap ='gray') scaled_img.shape (200, 200, 3)Ġ8 step #Run It All Together & Check it out import cv2 import numpy as np def resizeAndPad(img, size, padColor =0): h, w = img.shape sh, sw = size # interpolation method if h > sh or w > sw: # shrinking image interp = cv2.INTER_AREA else: # stretching image interp = cv2.INTER_CUBIC # aspect ratio of image aspect = w /h # compute scaling and pad sizing if aspect > 1: # horizontal image new_w = sw new_h = np.round(new_w /aspect).astype(int) pad_vert = (sh -new_h) /2 pad_top, pad_bot = np.floor(pad_vert).astype(int), np.ceil(pad_vert).astype(int) pad_left, pad_right = 0, 0 elif aspect < 1: # vertical image new_h = sh new_w = np.round(new_h *aspect).astype(int) pad_horz = (sw -new_w) /2 pad_left, pad_right = np.floor(pad_horz).astype(int), np.ceil(pad_horz).astype(int) pad_top, pad_bot = 0, 0 else: # square image new_h, new_w = sh, sw pad_left, pad_right, pad_top, pad_bot = 0, 0, 0, 0 # set pad color if len(img.shape) = 3 and not isinstance(padColor, (list, tuple, np.ndarray)): # color image but only one color provided padColor = *3 # scale and pad scaled_img = cv2.resize(img, (new_w, new_h), interpolation =interp) scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType =cv2.BORDER_CONSTANT, value =padColor) return scaled_imgĬhecking it out: new_img = resizeAndPad(img, (200,200), 127) i = plt.imshow(new_img, cmap ='gray') new_img.shape # Saving the new image inside DATA folder filename = 'tv_laptop_resized.jpg' # Using cv2.imwrite() method # Saving the image cv2.Pic = cv2.imread('itslinuxfoss.png', cv2.IMREAD_UNCHANGED)
0 Comments
Leave a Reply. |