Python 3 OpenCV Script to Compress or Reduce Image Size and Resize Image to Small Dimensions Full Project For Beginners

Python 3 OpenCV Script to Compress or Reduce Image Size and Resize Image to Small Dimensions Full Project For Beginners

 

Welcome folks today in this post we will be compressing image to lower size and also resize image using opencv library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install opencv-python

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

# import openCV library for image handling
import cv2

# read image to be resized by imread() function of openCV library
img = cv2.imread('screenshot.png')
print(img.shape)

# set the ratio of resized image
k = 5
width = int((img.shape[1])/k)
height = int((img.shape[0])/k)

# resize the image by resize() function of openCV library
scaled = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
print(scaled.shape)

# show the resized image using imshow() function of openCV library
cv2.imshow("Output", scaled)
cv2.waitKey(500)
cv2.destroyAllWindows()

# get the resized image output by imwrite() function of openCV library
cv2.imwrite('resized_output_image.jpg', scaled)

 

See also  Python 3 OS Module Script to Remove all Directories or Folders and Subdirectories Using rmdir() Method Full Project For Beginners

 

Here you need to replace the path of the input image in this above script and also you need to execute this python script by the following command

 

python app.py

 

 

Leave a Reply