


height )) else : size = DEFAULT_SIZE if not os. output_dir else : output_dir = dir + '/resized' if args. input_dir else : input_dir = dir + '/images' if args. add_argument ( '-t', '-height', help = 'Resized Height' ) args = parser. add_argument ( '-w', '-width', help = 'Resized Width' ) parser. add_argument ( '-o', '-output_dir', help = 'Full Output Path' ) parser. add_argument ( '-i', '-input_dir', help = 'Full Input Path' ) parser. We will need to import argparse before setting up our command-line arguments: We can also include parameters for the height and width of our resized images. This will give use the ability to do things like pass in the directory in which our existing images are stored, and pass in another directory of where we would like our new, resized images to go. That being said, in order to make this usable without having to change the script every time, we will use argparse to allow passing in parameters through the command line.

We now have a working bulk image resize process. Running the script should successfully resize any images that have been placed in our images folder. save ( new_file ) except IOError : print ( "unable to resize image ".

LANCZOS ) new_file = output_dir + "/" + outfile + extension img. open ( input_dir + '/' + infile ) img = img. splitext ( infile ) + "_resized" extension = os. The function does not alter the original image instead it returns the dimensions of image. def resize_image ( input_dir, infile, output_dir = "resized", size = ( 320, 180 )): outfile = os. To resize an image, the resize method is used in python. The original image with dimensions has been resized to using resize() function. The module also provides a number of factory functions, including functions to load images from files, and to create new images. As an example : import cv2 img cv2.imread ('pic.jpg') Cv2 function. The Image module provides a class with the same name which is used to represent a PIL image. resizedimage image.resize ( (600,600)) print (resizedimage.size) OUTPUT> (600, 600) Reading Images To read images in OpenCV, use a function cv2.imread ()where the first parameter is the image file name complete with its extension. Output Original Dimensions : (149, 200, 4) PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. Print('Resized Dimensions : ',resized.shape) Resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) Height = int(img.shape * scale_percent / 100) Width = int(img.shape * scale_percent / 100) Scale_percent = 60 # percent of original size Print('Original Dimensions : ',img.shape) Img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED) We will use this scale_percent value along with original image’s dimensions to calculate the width and height of output image. Providing a value <100 downscales the image provided. In the following example, scale_percent value holds the percentage by which image has to be scaled. Resize only the height (Increase or decrease the height of the image keeping width unchanged)įollowing is the original image with dimensions (149,200,4) (height, width, number of channels) on which we shall experiment on :Įxample 1 – Resize and Preserve Aspect Ratio Downscale with resize().Resize only the width (Increase or decrease the width of the image keeping height unchanged).Upscale (Increase the size of the image).Downscale (Decrease the size of the image).Preserve Aspect Ratio (height to width ratio of image is preserved).We will look into examples demonstrating the following resize operations. Resizing an image can be done in many ways. INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood But when the image is zoomed, it is similar to the INTER_NEAREST method. It may be a preferred method for image decimation, as it gives moire’-free results. INTER_NEAREST – a nearest-neighbor interpolation INTER_LINEAR – a bilinear interpolation (used by default) INTER_AREA – resampling using pixel area relation. flag that takes one of the following methods. The syntax of resize function in OpenCV is cv2.resize(src, dsize]]])
