How to edit an image with OpenCv in Python

How to edit an image with OpenCv in Python

In this article we will talk about how to edit an image with OpenCv in Python.

In the previous article we explained how to get started with OpenCv in Python.

Today we’ll see some more features!

In this article we will see how to edit an image with OpenCv in Python. We will learn how to resize an image and how to write and draw on it.

So let’s start!

Rescaling an Image with OpenCv

Rescaling an image we change it’s dimensions and to do that we can create our own function.

def rescaleFrame(frame, scale = 0.75):      #works for images, 
    width = int(frame.shape[1] * scale)
     #videos, live video
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    
    return cv.resize(frame, dimensions, interpolation = cv.INTER_AREA)

We can use the function rescaleFrame() to change the the dimensions of an image, a video or a live video and returns the new values for the frame.

  • frame is the image, video…that you want to change.
  • scale is the scale factor that you want to apply to rescale the frame.

We can define also another function only usefull for live videos. That’s because in that case the function is not directly changing the dimensions, but it’s capturing the frame with the dimensions that we want.

def changeRes(width, height):   #only works for live videos
    capture.set(3, width)   #3 is the number of the propriety 
                            #width
    capture.set(4, height)  #4 is the number of the propriety 
                            #height

The number 3 and 4 are the number of the propriety.

Now we will do an example:

import cv2 as cv
img = cv.imread('Photos/dog.jpg')
cv.imshow('Original Image', img)

def rescaleFrame(frame, scale = 0.75)
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)

    return cv.resize(frame, dimensions, interpolation = cv.INTER_AREA)

resized_image = rescaleFrame(img, 0.2)
cv.imshow('Resized Image', resized_image)

cv.waitKey(0)

Now you will see the same image with 2 different dimensions.

How to Draw on an image

Now we want to draw on an image, and we will do that by drawing some geometric shapes.

But first of all we will create a blank frame.

import cv2 as cv

blank[:] = 0,255,0    #create a blank frame and choose the color
                      #by editing 0,0,0 (RGB)
cv.imshow('Green', blank)

cv.waitKey(0)

If you want you can also paint a portion of the image with:

blank[200:300, 300:400] = 0,0,255

In that way you are saying:

  • from 200 to 300 in width
  • from 300 to 400 in height

Paint with the color Blu

Now we will draw on that blank frame.

I’ll put here some methods to draw a shape then you can use the the ones that you prefer.

#Draw Rectangle
cv.rectangle(blank, (0,0), (250,250), (255,0,0), thickness=2)

#Draw Circle
cv.circle(blank, (0,0), (250,250), 40, (0,0,255), thickness=3)

#Draw Line
cv.line(blank, (0,0), (250,250), (255,255,255), thickness=3)

Here I explain the parameter:

  • blank: name of the frame/image…on which you are drawing
  • (0,0) – (250, 250): Start and end points. (Try also to write blank.shape[1]//2 blank.shape[0]//2)
  • The 4th parameter is the color that you want

Example:

import cv2 as cv

blank[:] = 0,255,0                      

cv.circle(blank, (blank.shape[1]//2, blank.shape[0]//2), 40, (0,0,255), thickness=3)

cv.imshow('Img', blank)

cv.waitKey(0)

In the end let’s see how to write a text on our image.

cv.putText(blank, 'Hello', (0,255), cv.FONT_HERSHEY_TRIPLEX, 1.0, (255,255,255), thickness=2)

So for example:

import cv2 as cv

blank[:] = 0,255,0                      
cv.putText(blank, 'Hello', (0,255), cv.FONT_HERSHEY_TRIPLEX, 1.0, (255,255,255), thickness=2)

cv.imshow('Img', blank)

cv.waitKey(0)

That all for this Tutorial! In the next tutorial we will learn some other tools to change frames in OpenCv!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *