Images (reading, showing and writing)

A short introduction on how to read , show and write images using OpenCV.

Given below is a program in python to read ,display and write images.

#read ,show  and write images in opencv
import cv2
img=cv2.imread("C:\\Users\\amol\\Desktop\\goku.jpg",1)
print(img)
cv2.imshow("goku",img)
cv2.waitKey(4000)
cv2.destroyAllWindows()
cv2.imwrite('goku.jpg',img)

Functions used:

imread():

This function is used to read images.It takes two parameters : the path of image and the flag.
imread(give the path of the image ,flag)
flag – indicates the way the image will be read (0,1,-1)
1 : loads a color image
0 : loads image in gray scale mode
-1 : loads image as such including alpha channel

print(img)

will show u the following output:

imshow():

This function displays the image that has been read on the screen.It takes two parameters : the tittle of the window and the image variable.

imshow(window title, image variable)

waitKey():

waitKey() function will show the image for a specific amount of time.It takes time in millisecond as a parameter

If 0 is passed as the parameter then the window will remain open till you hit any random key

destroyAllWindows():

This function destroys all the windows which we have created

imwrite():

This function is used to save the image file that we have read. It take s two parameters: the file name and the image variable. Give the file name and its format and give the image variable as parameters. Look at the given picture below . The file has been saved (highlighted in yellow).

prerequisites: What is OpenCV?

One Comment

Leave a Reply