/Image-resizing

Image Resizing with Python

Primary LanguagePython

Image-resizing

Image Resizing with Python

Tools and Languages:

OpenCV

VS Code

pip

Python


Installation

Use the package manager pip to install cv2 and numpy.

pip install cv2
pip install numpy

Import

import cv2
import numpy as np

Reading image from file

Here we are using 'cv2.imread()' method to read samples.

img = cv2.imread("cat.png")

Img resizing

Theory:(Demonstrated Down-scaling)

Here we are using a bit better method to resize an image, preserving the aspect-ratio rather than using normal resizing.
1.Set the scale-percentage i.e percentage upto which, we want to downscale.
2.Build new width of the resized image i.e (Original image width * scale_percent).
3.Build new height of the resized image i.e (Original image height * scale_percent).
4.Storing height and width.
In the next snippet we are using interpolation to finally resizing it.

scale_percent = 0.60
width = int(img.shape[1]*scale_percent)
height = int(img.shape[0]*scale_percent)
dim = (width,height)

Implementing interpolation

As we are demonstrating Down-scaling or shrinking the scale, we will be using cv2.INTER_AREA method of Interpolation

resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)

Comparing original vs resized

cv2.imshow('ORIGINAL',img)
cv2.imshow('RESIZED',resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Images

Logo Resized

Developed by

Ashish ku. Behera