AppDividend
Latest Code Tutorials

How to Scale Images in Python using OpenCV

0

To upscale or downscale the image in Python, use cv2.resize() method. Image scaling is one of the most important operations in Computer Vision problems. Sometimes, the user wants to scale up the image to get more details about the specific object, and sometimes the user needs to scale down the images to fit some criteria.

Python cv2.resize()

To resize or scale an image in Python, use the cv2.resize() function. Scaling the image means modifying the dimensions of the image, which can be either only width, only height, or both. You can preserve the aspect ratio of the scaled image.

Resizing an image can be done in many ways. We will look into examples demonstrating the following resize operations.

  1. While preserving the aspect ratio (height to width ratio of the image is maintained),
    1. Downscale (Decrease the size of the image).
    2. Upscale (Increase the size of the image).
  2. While not preserving the aspect ratio,
    1. Resize an image only with width (Increase or decrease the width of the image keeping height unchanged).
    2. Resize an image only with height (Increase or decrease the height of the image keeping width unchanged).
  3. Resize the image to the specific width and height.

Downscale image with cv2.resize()

To downscale the image to half size, we can pass the fx and fy value to 0.5.

See the following code example.

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

img_half = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
cv2.imshow('Half Image', img_half)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Scale Images in Python

In this example, we used a resize() image to scale half the size of our original image. We will pass in a value of (img, ), and then we pass in the absolute size, or (0,0) to not set an absolute size in pixels, and then we can optionally pass in our relative factors of fx=0.5, fy=0.5This will output in the image that is half the size in both dimensions of the original.

Upscale image with cv2.resize()

To upscale the image, we can pass the fx and fy value to 1.5

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

img_scale_up = cv2.resize(img, (0, 0), fx=1.5, fy=1.5)

cv2.imshow('Upscaled Image', img_scale_up)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Upscale image with cv2.resize()

How to Stretch the Image in Python

To stretch the image in Python, we will pass the explicit dimensions on scaling the images.

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

img_stretch = cv2.resize(img, (600, 600))

cv2.imshow('Stretched Image', img_stretch)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Python using OpenCV

You can see that the right side image is stretched with respect to (600, 6000) dimensions.

Set an interpolation mode while scaling

To set an interpolation mode while scaling, pass the interpolation parameter. We will use the nearest interpolation mode instead of the default mode.

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

img_inter = cv2.resize(img, (600, 600), interpolation=cv2.INTER_NEAREST)

cv2.imshow('Nearest Interpolated Image', img_inter)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Interpolation in Python cv2

Scale image horizontally in Python

To scale the image horizontally using OpenCV, scale the image only along the x-axis or horizontal axis, and keep the height of the image unchanged.

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

new_width = 300

dim_size = (new_width, img.shape[0])

horizon_img = cv2.resize(img, dim_size, interpolation=cv2.INTER_AREA)

cv2.imshow('Horizontal Scaled Image', horizon_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Scale image horizontally

Scale image vertically in Python

To scale the image vertically using OpenCV, scale the image only along the y-axis or vertical axis, and keep the width of the image unchanged.

# app.py

import numpy as np
import cv2

img = cv2.imread('data.png', 1)
cv2.imshow('Original', img)

new_height = 600

dim_size = (new_height, img.shape[1])

horizon_img = cv2.resize(img, dim_size, interpolation=cv2.INTER_AREA)

cv2.imshow('Vertically Scaled Image', horizon_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Scale image vertically in Python

Conclusion

To scale image in Python, use cv2.resize() method. We have seen scenarios where we can either preserve the aspect ration and change the dimension or don’t preserve. We have scaled the image horizontally and vertically. That is it for Scaling an Image in Python.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.