Resampling is performed on all pixels that may have an impact on the outcome. Using Pillow’s Image class resize method, we can resize an image to a specific width and height.As its elements, this tuple contains the image’s width and height. The function does not alter the original image instead it returns the dimensions of image. To resize an image, the resize method is used in python.The module also includes several factory functions, such as those for loading images from files and creating new images. The Image module includes a class of the same name that is used as PIL.Web development, programming languages, Software testing & others I’ll probably be adding a GitHub link soon and maybe a video link too.Start Your Free Software Development Course I hope you guys found this easy to follow and fun to code. By default, the interpolation method cv.INTER_LINEAR is used for all resizing purposes. Preferable interpolation methods are cv.INTER_AREA for shrinking and cv.INTER_CUBIC (slow) & cv.INTER_LINEAR for zooming. If you’re wondering why we pass in cv2.INTER_AREA for our interpolation in the cv2.resize function, here’s a quick explanation from their official docs: Different interpolation methods are used. Else (if you only pass in a new width value) it calculates the new dimensions while keeping the aspect ratio.Else if you only pass in a new height value it calculates the new dimensions while keeping the aspect ratio.Else if you pass in both width and height parameters it sets the dimensions to (width, height) without keeping the aspect ratio.If you don’t pass in any parameters, it returns the original image without resizing,.Here’s the code for the function: def resizeImg(image, width=None, height=None): dim=None (h,w) = image.shape if width is None and height is None: return image elif width is not None and height is not None: dim = (width, height) elif width is None: r = height/float(h) dim = (int(w*r), height) else: r = width / float(w) dim = (width, int(h*r)) resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) return resizedīasically what the code above do is it calculates the dimensions (dim) based on what inputs you give it: Then for the rest of the function we’ll just have to get the new dimensions based on inputs and calculated aspect ratios and then pass the image and dimensions through the cv2.resize() function and we’re done. So now we have something like this: def resizeImg(image, width=None, height=None) So the three parameters we want to pass in are: Resize both height and width (and lose aspect ratio).Okay now moving on to the resizeImg() function. Alternatively you can also have a condition where if this folder isn’t found then create it but this isn’t included in this tutorial. Note that you have to create a new folder (in my case mine is “./newResizedImgs/”) first to store all the new resized images if not this wouldn’t work. In our main function, what we aim to do is to read each image file (filename) in the “./originalImgs/” folder with cv2.imread, then if it’s not None, we pass it through the resizeImg function before saving it to our new folder (“./newResizedImgs/”). Okay so in the script above we have one main function and one resizeImg function. Once that’s done, we can start writing our python script (let’s first write the main function first): from cv2 import cv2 import os def resizeImg(): # we'll fill in this section below return def main(): folder = "./originalImgs/" newResizedFolder = "./newResizedImgs/" for filename in os.listdir(folder): img = cv2.imread(os.path.join(folder, filename)) if img is not None: #newImage = resizeImg() #we'll fill in this later newImgPath = newResizedFolder + filename cv2.imwrite(newImgPath, newImage) if _name_ = "_main_": main()
I use pip to install python-opencv so in your terminal (remember to cd into your working directory), just type pip install opencv-python (after the $): your-project-directory$ pip install opencv-python This awesome library comes with many functions you can call out-of-the-box so if you’re interested in anything related to image processing, do check it out!
The only library you’ll need to install is OpenCV.