Home » Python

Upscaling the RGB image in Python

Python image processing example: In this article we will see how to upscale the RGB image in Python?
Submitted by Ankit Rai, on June 05, 2019

Upscaling of an image refers to enlarging the size of an image.

In this program, we will be using two functions of OpenCV-python (cv2) module.. let's see their syntax and descriptions first

1) imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.

If flag value is:

  • 1: Loads a color image.
  • 0: Loads image in grayscale mode.
  • -1: Loads image as such including alpha channel.

If the flag value is not given then show the original image, which path is given.

2) imshow():
It takes window name and image matrix as an argument in order to display an image in a display window with a specified window name.

Also In this program, we will be using one attribute of an image matrix:

3) shape: This is the attribute of an image matrix which returns the shape of an image i.e. consisting of the number of rows, columns, and the number of planes.

Python program for upscaling the RGB image

# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2

# import numpy library as np into this program
import numpy as np

# define a function for upscaling the image
def upscaling(img,x,y,row,col) :

    # here image is of class 'uint8', the range of values  
    # that each colour component can have is [0 - 255]

    # create a zero matrix of order of x,y times
    # of previous image of 3-dimensions
    upscaling_img = np.zeros((x*row,y*col),np.uint8)

    i, m = 0, 0

    while m < row :

        j, n = 0, 0
        while n < col:

            # We assign pixel value from original image matrix to the
            # new upscaling image matrix in alternate rows and columns
            upscaling_img[i, j] = img[m, n]

            # increment j by y times
            j += y

            # increment n by one
            n += 1

        # increment m by one
        m += 1

        # increment i by x times
        i += x

    return upscaling_img

       

# Driver Code
if __name__ == "__main__" :
    
    # read an image using imread() function of cv2
    # we have to  pass only the path of the image
    img = cv2.imread(r'C:/Users/user/Desktop/pic6.jpg')

    # displaying the image using imshow() function of cv2
    # In this : 1st argument is name of the frame
    # 2nd argument is the image matrix
    cv2.imshow('original image',img)

    # assigning number of rows, coulmns and
    # planes to the respective variables
    row,col,plane = img.shape

    # assign Blue plane of the BGR image
    # to the blue_plane variable
    blue_plane = img[:,:,0]

    # assign Green plane of the BGR image
    # to the green_plane variable
    green_plane = img[:,:,1]

    # assign Red plane of the BGR image
    # to the red_plane variable
    red_plane = img[:,:,2]

    # Upscaling the image x,y times along row and column
    x,y = 2, 2

    # here image is of class 'uint8', the range of values  
    # that each colour component can have is [0 - 255]

    # create a zero matrix of order of x,y times
    # of previous image of 3-dimensions
    upscale_img = np.zeros((x*row,y*col,plane),np.uint8)
    
    upscale_img[:,:,0] = upscaling(blue_plane, x,y,row,col)
    upscale_img[:,:,1] = upscaling(green_plane, x,y,row,col)
    upscale_img[:,:,2] = upscaling(red_plane, x,y,row,col)

    cv2.imshow('Upscale image',upscale_img)

Output

Upscaling the RGB image in Python - output



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.