Resizing and fliping codes

Resizing and Flipping images would be very useful when you are working on creating new database for training a CNN, RNN or deep learning algorithms. For example I was working with many images that my Prof captured from wheat plants. We were going to apply CNN algorithm to that images. But the number of the images was too low so I put them in an Folder and ran the below code.

import glob
import cv2
file_path = glob.glob("/content/drive/My Drive/Data/Yellow/*")
for file in file_path:
  print(file)
  img = cv2.imread(file)
  #crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}
  # perform the actual resizing of the image and show it
  #resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  img_rotate_90_clockwise = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_clockwise.jpg', img_rotate_90_clockwise)
  # True

  img_rotate_90_counterclockwise = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
  cv2.imwrite(file+'_cv_rotate_90_counterclockwise.jpg', img_rotate_90_counterclockwise)
  # True

  img_rotate_180 = cv2.rotate(img, cv2.ROTATE_180)
  cv2.imwrite(file+'_cv_rotate_180.jpg', img_rotate_180)

As you can see I tried to find all of the files path using “glob” library. Next, I tried to read the images using “imread” function. Also trying to resize them using “resize” function. Finally , save the files using “imwrite” function.

More information

Don not forget that you need opencv library to do these stuff. if you are new in image processing go to our instagram page. Also you can see our related pages here.