Resize Images using google Colab GPU

Rojan Dhimal
1 min readOct 8, 2020

If you have large image datasets it very time-consuming for you to resize in CPU. In this article I will show you how I resize images using google colab free gpu.

  • Create new google colab file
  • create github repo to store image dataset
  • clone you repo in google colab
  • run the code

You can create colab file from here https://colab.research.google.com/#create=true
after creating colab file click on Edit>Notebook settings>set GPU

or you can get my code here https://colab.research.google.com/drive/1sMC5xOTRAPBewNH4BG32vcSWg7ZoYt_g?usp=sharing

Now clone your git hub repository where your dataset is or just create folder and store images

import cv2
import numpy as np
import os
PATH = os.getcwd()
resize_all_image_path = PATH + '/images' #SOURCE PATH
print(resize_all_image_path)
new_cat = []
for x in os.listdir(resize_all_image_path):
new_cat.append(x)
print(x)
IMG_SIZE = 32
def resize_all_image():
for catagory in new_cat:
path = os.path.join(resize_all_image_path, catagory)
for img in os.listdir(path):
try:
img_array =cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
x=str(img)
img_loc='newimages/'+catagory+'/'+x #DESTINATION PATH
cv2.imwrite(img_loc,new_array)
except Exception as e:
pass
print("Resized and shave character {}".format(catagory))
resize_all_image()
print("Resized and saving Completed!")

That’s it. Thank you for reading.

--

--