# Upscaling the grayscale image in Python

In this article, we will see **how to upscale the greyscale image using image processing in Python?**

Submitted by Ankit Rai, on June 02, 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 function of **numpy module**.

**median():** It takes array and returns the median of the array .

Also, in this program we are using the concept of array slicing

Let, A is 1-d array:

A[start:stop:step]

**start:**Starting number of the sequence.**stop:**Generate numbers up to, but not including this number.**step:**Difference between each number in the sequence.

**Example:**

A = [1,2,3,4,5,6,7,8,9,10] print(A[ 1: 5]) Output: [2,3,4,5]

### Python program for upscaling the grayscale image in Python

# open-cv library is installed as cv2 in python # import cv2 library into this program import cv2 # import numpy as np name import numpy as np # 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',0) # 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) # upscaling code # 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*img.shape[0],y*img.shape[1]),np.uint8) i, m = 0, 0 while m < img.shape[0] : j, n = 0, 0 while n < img.shape[1]: # We assign pixel value from original image matrix to the # new upscaling image matrix in alternate rows and columns upscale_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 cv2.imshow('upscaling image',upscale_img)

**Output**

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL