Tuesday, July 28, 2020

OpenCV functions for Reading, Showing, Writing an Image File

Most of the CV applications need to get the images as input and produce the images as output. OpenCV provides the following functions for this purpose:

  •  imread() function: This is the function for reading an image. OpenCV imread() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
  •  imshow() function: This is the function for showing an image in a window. The window automatically fits to the image size. OpenCV imshow() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
  •  imwrite() function: This is the function for writing an image. OpenCV imwrite() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

Example

This example shows the Python code for reading an image in one format: showing it in a window and writing the same image in other format. Consider the steps shown below:

Import the OpenCV package as shown:

import cv2

Now, for reading a particular image, use the imread() function:

image = cv2.imread('image_flower.jpg')

For showing the image, use the imshow() function. The name of the window in which you can see the image would be image_flower.

cv2.imshow('image_flower',image)
cv2.destroyAllwindows()


Now, we can write the same image into the other format, say .png by using the imwrite() function:

cv2.imwrite('image_flower.png',image)

The output True means that the image has been successfully written as .png file also in the same folder.

True

Note: The function destroyallWindows() simply destroys all the windows we created.
Share:

0 comments:

Post a Comment