asked 163k views
2 votes
Code in python to Iterate over all these images and resize each

image to the following interpolations: • Interpolation: i)Bilinear
ii. Nearest iii. Area iv. Bicubic

1 Answer

2 votes

In Python, iterating over all the images and resizing each image to the following interpolations can be done using the OpenCV library. The following code can be used:import cv2import osimg_folder = 'folder_path'new_folder = 'new_folder_path'interpolations = [cv2.INTER_LINEAR, cv2.INTER_NEAREST, cv2.INTER_AREA, cv2.INTER_CUBIC]for filename in os.listdir(img_folder): img_path = os.path.join(img_folder, filename) img = cv2.imread(img_path) for interp in interpolations: new_img = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=interp) new_filename = filename.split('.')[0] + '_' + interp + '.' + filename.split('.')[1] new_img_path = os.path.join(new_folder, new_filename) cv2.imwrite(new_img_path, new_img)The above code reads all the images in the specified folder and resizes each image to the specified interpolations: bilinear, nearest, area, and bicubic. The new images are then saved in the specified new folder with the name of the original image appended with the interpolation method used to resize the image.

answered
User Reena Shirale
by
8.7k points