In this Python OpenCV article iam going to show you Working with Keyboards. also we will learn how you can write or save an image in OpenCV, so first of all let’s talk about OpenCV.
What is Python OpenCV ?
Python OpenCV (Open Source Computer Vision) is a library for computer vision and image processing tasks. it is an open source computer vision and machine learning software library written in C++ with bindings for Python, Java and C#. it provides different functions and algorithms for image and video analysis, object detection, face recognition and machine learning.
OpenCV was originally developed by Intel and later supported by Willow Garage and Itseez. this library has large community of developers and users, and it is widely used in industry, academia and research.
Python OpenCV provides comprehensive set of tools for working with images and video streams, including reading and writing image files, manipulating images, applying filters and transformations, performing feature detection and matching and many more. it also provides support for real time video processing and camera control.
Key Features of Python OpenCV ?
OpenCV (Open Source Computer Vision) is popular computer vision and machine learning library that has extensive support for Python. these are some key features of Python OpenCV:
- Image and Video Processing: Python OpenCV provides different functions and algorithms to manipulate images and videos, including basic image operations, filtering, feature detection and object recognition.
- Object Detection: OpenCV has several pre trained classifiers for object detection tasks such as face detection, eye detection and pedestrian detection. these classifiers are based on machine learning algorithms like Haar cascades and deep neural networks.
- Machine Learning: OpenCV provides set of tools and libraries for machine learning tasks such as classification, clustering and regression. it also includes support for popular machine learning frameworks like TensorFlow and PyTorch.
- Real-time Processing: OpenCV has support for real time processing of video streams, making it useful for applications like surveillance, robotics and video analysis.
- Cross-platform Support: Python OpenCV is cross platform and can be used on different operating systems like Windows, Linux and macOS. it also supports different programming languages such as Python, C++ and Java.
- Integration with Other Libraries: OpenCV can be integrated with other popular libraries such as NumPy and Matplotlib to perform advanced image processing and visualization tasks.
In result we can say that Python OpenCV is powerful library for computer vision and machine learning tasks. it has rich set of features and it is easy of use.
Also you can check Python GUI Development Tutorials in the below link.
- PyQt5 GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development Tutorials
- Kivy GUI Development Tutorials
So this is the complete source code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import cv2 # Creating A Simple Application img = cv2.imread("lena.tif") cv2.imshow("Lena Image", img) k= cv2.waitKey(0) if k == 27: # wait for esckey to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for s key to save and exit cv2.imwrite("JPGNewImage.jpg", img) cv2.destroyAllWindows() |
in the first we have imported OpenCV, after that read the image and show the image. in the if statements we are going to check some conditions the first condition if k==27 it means that we are waiting for the escape key to exit and in elif == ord(‘s’) it means that wait for s key to save and exit.
This is the code that we are checking for the keyboard input, if it was escape key we are going to destroy the window. and if it was the ‘s’ key we are saving our image, you can see that in the cv2.imwrite() we are giving the image name with our image.
1 2 3 4 5 6 |
if k == 27: # wait for esckey to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for s key to save and exit cv2.imwrite("JPGNewImage.jpg", img) cv2.destroyAllWindows() |
Run the complete code and this will be the result.

Also you can watch the complete video for this article