Python to Reduce Image Size with Full Source Code For Beginners

Script to reduce the size of image files using the OpenCV library of python.


  • openCV library
pip install opencv-python

Run the Script:

  • Add the image in jpg format with name as ‘input.jpg’ in this folder.
  • Run script.
  • resized output image will be generated in this folder.

Source Code:

# import openCV library for image handling
import cv2

# read image to be resized by imread() function of openCV library
img = cv2.imread('input.jpg')

# 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)

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

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



Leave a Comment