#7 Opencv Images: Building Modern GUIs using Python | Full Course | 2025

THE SUMMARYAI-generated

Key Concepts

  • OpenCV image integration in PI visual
  • PI visual element properties (fill image, background color, corner radius, border)
  • Updating PI visual using pip install pi visual --upgrade
  • Attaching events in PI visual
  • Using cv2.imread() to read images
  • File dialog integration for image selection
  • Using partial from functools to pass UI to callback functions
  • Image processing with OpenCV (e.g., grayscale conversion)

OpenCV Image Integration in PI visual

The video demonstrates how to integrate OpenCV images into a PI visual application. The process involves using the "OpenCV image" element available in PI visual's advanced tab. This element allows displaying and manipulating images using OpenCV functions within the PI visual environment.

PI visual Element Properties

The "OpenCV image" element has several customizable properties:

  • Fill Image: Determines whether the image should be zoomed to fill the entire container or maintain its aspect ratio. If enabled (blue), the image will zoom to fill the container, potentially cropping parts of the image. If disabled, the image will maintain its aspect ratio, and the background color will be visible in the remaining space.
  • Background Color: Sets the background color of the element.
  • Corner Radius: Defines the roundness of the corners of the element.
  • Border Color and Thickness: Sets the color and thickness of the border around the element.
  • Flip: Flips the image vertically or horizontally.
  • Opacity: Adjusts the transparency of the image.

Setting up the PI visual Project

  1. Create a new project: Create a new PI visual project and name it (e.g., "OpenCV image").
  2. Update PI visual: Ensure you have the latest version of PI visual by running pip install pi visual --upgrade in your terminal. This is crucial because PI visual is frequently updated.
  3. Create a directory: Create a new directory within your project to store the PI visual files (e.g., "opencv image").
  4. Copy the path: Copy the path to the newly created directory.
  5. Update and Autosync: In PI visual, update the project settings with the directory path and enable autosync to automatically reflect changes in your code.

Displaying a Static Image

  1. Import OpenCV: Import the OpenCV library in your Python code using import cv2.
  2. Read the image: Use cv2.imread() to read an image from a file path. For example: img = cv2.imread(r"path/to/your/image.jpg"). The r prefix ensures that the path is treated as a raw string, preventing issues with backslashes.
  3. Access the OpenCV image element: In the attach_events function, access the OpenCV image element using its variable name (e.g., opencv_image).
  4. Set the image: Use UI.page_0.opencv_image.image = img to set the image of the element to the OpenCV image you loaded.

Integrating a File Dialog for Image Selection

  1. Add a "Browse" button: Add a "Browse" button from the "Inputs" -> "File Dialogs" section in PI visual.
  2. Customize the button: Customize the button's appearance (color, border, fill, etc.) as desired.
  3. Name the button: Assign a variable name to the button (e.g., "button_browse").
  4. Define a callback function: Create a function (e.g., set_image) that will be called when a file is selected using the browse button. This function should take the file path as an argument.
  5. Use on_file_selected: In the attach_events function, use UI.page_0.button_browse.on_file_selected = partial(set_image, UI=UI) to connect the button's on_file_selected event to the set_image function.
  6. Import partial: Import the partial function from the functools module: from functools import partial.
  7. Pass UI using partial: Use partial to pass the UI object to the set_image function. This is necessary because the callback function needs access to the PI visual elements.
  8. Read the image in the callback: In the set_image function, read the image using cv2.imread(path) and then set the image of the OpenCV image element: UI.page_0.opencv_image.image = img.

Image Processing Example: Grayscale Conversion

The video demonstrates a simple image processing example: converting an image to grayscale.

  1. Convert to grayscale: Use cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) to convert the image to grayscale.
  2. Display the grayscale image: Set the image of the OpenCV image element to the grayscale image: UI.page_0.opencv_image.image = gray.

Code Example

import cv2
from functools import partial

def set_image(path, UI=None):
    img = cv2.imread(path)
    UI.page_0.opencv_image.image = img

def attach_events(UI):
    UI.page_0.button_browse.on_file_selected = partial(set_image, UI=UI)

    # Initial image loading (optional)
    # img = cv2.imread(r"path/to/your/default_image.jpg")
    # UI.page_0.opencv_image.image = img

Challenge

The video concludes with a challenge to the viewers: create a simple image editor using the demonstrated techniques. This editor should allow users to upload an image and apply different image processing techniques (e.g., grayscale, HSV conversion) using buttons.

Conclusion

The video provides a practical guide to integrating OpenCV images into PI visual applications. It covers the essential steps, from setting up the project to implementing file dialog integration and basic image processing. The use of partial to pass the UI object to callback functions is a key takeaway. The challenge encourages viewers to further explore the possibilities of combining PI visual and OpenCV for creating interactive image processing applications.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.