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 Capture Image From Camera

Capture Image From Camera Using OpenCV, you can capture video from your computer's webcam and use these videos for many purposes. OpenCV provides a very simple interface for video capture process. It takes preparation to capture video. To capture a video, you must create a VideoCapture object. Its argument may be the device index. capture = cv2.VideoCapture( 0 )   The device index is just the number that determines which camera to use. Normally 0 is selected if a camera will be connected. You can select the second camera by giving a value of 1. After that, you can shoot frame by frame. ret , frame = capture.read()   cap.read () returns a bool value. If the frame reads correctly, the value will be True. With the Cap.isOpened () method, you can check whether the video capture process is started. If it's true, it's okay. If not, you need to open it using cap.open (). The codes are all as follows: import cv2 capture = cv2.VideoCapture( 0 ) while ( True ):   # C

Show OpenCV Image With Matplotlib

Show Image With Matplotlib Matplotlib is a drawing library that offers a wide variety of drawing methods for Python. Matplotlib; The basic python library we use in data visualization. It allows us to make 2 and 3 dimensional drawings. In order to use Matplotlib, we need to add it to our project as follows: from matplotlib import pyplot as plt The image is read as follows: img = cv2.imread( 'bird.jpg' )   Finally, the captured image is displayed on the screen with the following code: plt.imshow(img) plt.xticks([]) , plt.yticks([]) plt.show() Code and output are as follows: import cv2 from matplotlib import pyplot as plt img = cv2.imread( 'bird.jpg' ) plt.imshow(img) plt.xticks([]) , plt.yticks([]) plt.show() Output: