Skip to main content

Opencv Read-Write-Display Image Python

Read-Write-Display Image

In this lesson, you will learn how to read, how to view and save. The functions cv2.imread (), cv2.imshow (), cv2.imwrite () will be used for these operations.

To read an image, the function cv2.imread () is used. The image to be read must be in the working directory or a full visual path must be given. The second argument is a sign that indicates how the image should be read.

cv2.IMREAD_COLOR: Indicates that a color image will be loaded. It is the default flag. The integer value for this flag can be assigned to 1.

cv2.IMREAD_GRAYSCALE: Loads the image in grayscale mode.

cv2.IMREAD_UNCHANGED: Loads the image as included in the alpha channel.

import cv2
# The code below reads the image and assigns it to the image variable.
image = cv2.imread('bird')

The function cv2.imshow () is used to display an image. The window opens automatically in image size. The first argument is the name of the window to open. The second argument is the image we read above. We can create as many windows as we want with different window names.

cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The function cv2.imwrite()  is used to save the image. Used as follows.

cv2.imwrite("recorded.jpg",image)

cv2.waitKey () is a keyboard binding function. The argument it takes is the time in milliseconds. This function waits for the specified milliseconds for any keyboard event. If the argument is passed 0, it will wait indefinitely for a keystroke.

cv2.destroyAllWindows () destroys all created windows. If you want to destroy the previously specified window, the function cv2.destroyWindow () is used. Here, the full window name should be given as an argument.

The complete code is as follows:

import cv2
# The code below reads the image and assigns it to the image variable.
image = cv2.imread('bird.jpg')
cv2.imshow("Image", image)
cv2.imwrite("recorded.jpg",image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output of the code:



Comments

Popular posts from this blog

OpenCV Add Border to Image in Python

How to add border to image in python? To create a frame around the image, the function cv2.copyMakeBorder () is used. This function takes the following arguments: src - login image top, bottom, left, right - edge width in number of pixels in corresponding directions borderType - defines what type of border to add. There may be the following types cv2.BORDER_CONSTANT  cv2.BORDER_REFLECT - cv2.BORDER_REFLECT_101 cv2.BORDER_REPLICATE cv2.BORDER_WRAP  value  - Color of border if border type is cv2.BORDER_CONSTANT import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [ 255 , 0 , 0 ] img1 = cv2.imread( 'flower.jpg' ) replicate = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1 ...

How to create trackbar in Opencv Python?

Create trackbar in Opencv and Python import cv2 import numpy as np def func ( x ): pass img = cv2.imread( "flower.jpg" ) cv2.namedWindow( 'image' ) cv2.createTrackbar( 'R' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'G' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'B' , 'image' , 0 , 255 , func) switch = '0 : OFF \n 1 : ON' cv2.createTrackbar(switch , 'image' , 0 , 1 , func) while ( 1 ): cv2.imshow( 'image' , img) k = cv2.waitKey( 1 ) & 0xFF if k == 27 : break r = cv2.getTrackbarPos( 'R' , 'image' ) g = cv2.getTrackbarPos( 'G' , 'image' ) b = cv2.getTrackbarPos( 'B' , 'image' ) s = cv2.getTrackbarPos(switch , 'image' ) if s == 0 : img[:] = 0 else : img[:] = [b , g , r] cv2.destroyAllWindows() Trackbar