![]() resample is the filter that has to be used for resampling.This is the size requested for the resulting output image after resize. size is to passed as tuple (width, height).The syntax of resize() method is as shown in the following code snippet. In this tutorial, we shall learn how to resize an image using PIL, with example Python programs. You can pass parameters like resulting image size, pixel resampling filter and the box region of source to be considered. To resize an image with Python Pillow, you can use resize() method of Class. Python – Resize Image using Pillow library Example 2: Resize Image with only box of the input image.Example 1: Resize an image with default values.I’ll probably be adding a GitHub link soon and maybe a video link too. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |