PYTHON & OpenCV

Photo of author

By Jackson Taylor

Home » Examples » Open Cv » PYTHON & OpenCV

As an Amazon Associate we earn from qualifying purchases.

Python is a good first language for hardware projects, and OpenCV is the library that lets it work with images and video. Together they let a Raspberry Pi or a PC read a camera, open and edit pictures, and make simple decisions based on what the camera sees. This page is a learning path: it collects our Python tutorials in a sensible order, from basic syntax to image processing and electronics.

What OpenCV does in Python

OpenCV is an open-source computer vision library. In Python you use it through the cv2 module. With it you can load an image from disk, show it in a window, grab frames from a camera, change colours, resize or crop, and save the result as a new file.

These are the building blocks of larger projects such as face detection, object tracking and security cameras. You do not need to know the maths behind them to start. You do need to be comfortable with variables, conditions and loops, which is why the basics come first below.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Before you start: set up Python and pip

You need Python installed and a working package manager called pip. pip downloads libraries such as OpenCV and NumPy, which OpenCV uses to store image data. If you are on Windows and pip is not available yet, this guide shows how to install it from the command line:

Once pip works, OpenCV is normally installed with the package named opencv-python. After that, import cv2 at the top of a script tells you whether the install worked. If Python reports that the module is missing, the library went into a different Python installation than the one you are running.

Python basics

If you are new to Python, work through these lessons in order. They cover running your first program, printing text, doing arithmetic, joining strings, reading user input and repeating actions with loops. Every image or camera script relies on these ideas.

See also
230V to 110V ,200W SOLID STATE TRANSFORMER

The if statement matters most for vision work, because you will use it to react to a key press or to a result from the camera. The while loop is what keeps a camera feed running frame after frame.

Image processing with Python and OpenCV

These four tutorials take you from the camera to a saved file. A sensible order is to open an existing image first, because it removes the camera from the equation while you learn the basics.

The core functions

Most beginner scripts use the same few calls:

  • imread loads an image from a file path. If the path is wrong it does not raise an error; it returns nothing, so check the result before using it.
  • imshow displays an image in a window.
  • waitKey pauses for a key press. Without it, the window usually closes before you can see it.
  • imwrite saves an image to a new file. The file extension you choose decides the format.
  • VideoCapture opens a camera. Camera number 0 is normally the first camera connected.

Capturing from a camera

To capture a picture, open the camera, read a frame, save it with imwrite and release the camera. The tutorial on capturing when a key is pressed extends this idea. The script shows the live feed in a loop and saves a frame only when you press a chosen key. Checking the key inside an if statement is a pattern you will reuse in almost every camera project.

Using OpenCV on a Raspberry Pi

The Python code is the same on a Raspberry Pi as on a PC, so what you learn here carries over. A USB webcam is usually the simplest camera to start with, since VideoCapture can open it directly. Ribbon-cable camera modules can need extra setup depending on your Raspberry Pi OS version, so check the documentation for your release.

See also
555 ASTABLE

A Pi has less processing power than a laptop, so start with small images and simple processing. If a live feed is slow, reducing the frame size is the first thing to try. Once you are comfortable, drowsiness detection with OpenCV is a good practical project to build on these skills.

Advanced Python: email and online data

Python can also send results out of your project. These tutorials show how to send an email, how to attach a file (a captured image is a natural example) and how to send readings to an online chart.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Combining these ideas gives you useful projects, such as a camera that emails a photo when something happens, or a script that lets you plot sensor data online.

Python works related to electronics

Python can talk to hardware over a serial connection. These tutorials cover sending and receiving serial data, switching an LED from Python through an Arduino, and reading temperature from an LM35 sensor.

If you want to go further with sensor readings, our guide to Arduino data analysis shows what you can do with the numbers once Python has them.

Where to go next

After the tutorials above, build something small that combines a camera with a decision: save a photo on a key press, or send an email when your script finds what it is looking for. For more ideas at every level, browse these Raspberry Pi Python projects. If you are deciding which language to learn for hardware work, this comparison of C language vs Python explains the trade-offs.

FAQ

Do I need a Raspberry Pi to learn Python and OpenCV?

No. Everything in the image tutorials runs on a Windows, Mac or Linux computer with a webcam. A Raspberry Pi is useful when you want a small, low-power device that stays connected to hardware.

See also
PYTHON

What is cv2?

cv2 is the name you import in Python to use OpenCV. It is installed through the opencv-python package, even though you import it as cv2.

Why does my image window close immediately?

The script probably ends before you can see the window. Add a waitKey call after imshow so the program waits for a key press before closing.

Why does imread return nothing?

The file path is usually wrong, or the file is not where the script is running. Check the file name, the extension and the current folder, or use the full path to the image.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works