Python OpenCV imencode()
function
converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier.
Basic example of
imencode() Function
Example 1:
We began by importing the necessary libraries, which are
OpenCV
and
NumPy
. After importing libraries, we use the
imread()
function to load the image, using the image path as an argument. After loading the image, we begin encoding it with the imencode() method, passing the extension of the image to be encoded as well as the loaded image as parameters.
The outcome will differ depending on the format. If you notice, we are only saving the data of the first index of the imencode() method since it produces two outputs: whether the operation was successful or not at the zero index, and the encoded image at the first index. Now we’ll convert the encoded image to a NumPy array so we can use it. Finally, we convert this NumPy array to bytes so that it can be easily transferred.
Used image:
gfg.png
Code:
Python3
# If the extension of our image was
# '.jpg' then we have passed it as
# argument instead of '.png'.
img_encode
=
cv.imencode(
'.png'
, img)[
1
]
# Converting the image into numpy array
data_encode
=
np.array(img_encode)
# Converting the array to bytes.
byte_encode
=
data_encode.tobytes()
print
(byte_encode)
Output:
(Because the output was lengthy, only a portion of it is displayed here)
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
Python OpenCV - setWindowTitle() Function
Python OpenCV setWindowTitle() method used for giving the title of the windows. It takes 2 parameters that are windows name and the title that needs to be given. Both the parameters are expected to be of string type. Syntax: cv2.setWindowTitle( winname, title ) Parameters: winname: windows nametitle: title we want to set for the window with the abo
Python OpenCV - resizeWindow() Function
resizeWindow() method in Python OpenCV is used to resize window displaying images/videos to a specific size. The specified window size is for images excluding toolbars. This only works for created windows having flags other than CV_WINDOW_AUTOSIZE. Syntax: cv2.resizeWindow(window_name, width, height) Parameters: window_name: Name of the window that
Python OpenCV - waitKey() Function
waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Examples 1: Display image with a time limit Using waitKey() method
Python OpenCV - waitKeyEx() Function
Python OpenCv waitKeyEx() method is similar to waitKey() method but it also returns the full key code. The key code which is returned is implementation-specific and depends on the used backend: QT/GTK/Win32/etc. Syntax: cv2.waitKey(delay) Parameters: delay: The time in milliseconds after which windows needs to destroyed. If given 0 it waits for inf
Python OpenCV - getRotationMatrix2D() Function
cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image. Syntax: cv2.getRotationMatrix2D(center, angle, scale) Parameters: center: Center of rotationangle(θ): Angle of Rotation. Angle is positive for anti-clockwise and negative for clockwise.scale: scaling factor which scales the image
Python OpenCV - destroyAllWindows() Function
Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function. It doesn't take any parameters and doesn't return anything. It is similar to destroyWindow() function but this functi
Python OpenCV - namedWindow() Function
Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen. Created windows are referred by their names and can also be used as a placeholder. The function d
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy
&
Privacy Policy
Got It !
Please go through our recently updated
Improvement Guidelines
before submitting any improvements.
This improvement is locked by another user right now.
You can suggest the changes for now and it will be under 'My Suggestions' Tab on Write.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please go through our recently updated
Improvement Guidelines
before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.