Sunday, September 27, 2015

Capture Raspberry Pi Camera image, display on OpenCV, Matplotlib PyPlot and Tkinter GUI

This example capture photo from Raspberry Pi Camera Module, and display with OpenCV, Matplotlib PyPlot and Tkinter GUI.

python 1 - display wiyh OpenCV window
python 2 - display with matplotlib
python 3 - display with Tkinter

import picamera
import picamera.array
import time
import cv2
from matplotlib import pyplot as plt
import Tkinter 
import Image, ImageTk
import sys

def capturePiCam():
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        global img
        img =cap.array

#- display on OpenCV window -
def displayAtOpenCV():
    cv2.namedWindow('imageWindow', cv2.WINDOW_AUTOSIZE)

#- display with matplotlib -
def displayAtPyplot():
    plt.figure().canvas.set_window_title("Hello Raspberry Pi")
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
#- display on Tkinter -
def displayAtThinter():
    root = Tkinter.Tk() 
    b,g,r = cv2.split(img) 
    img2 = cv2.merge((r,g,b))
    img2FromArray = Image.fromarray(img2)
    imgtk = ImageTk.PhotoImage(image=img2FromArray) 
    Tkinter.Label(root, image=imgtk).pack() 

def displayUsage():
    print("usage: ")
    print("python 1 - display wiyh OpenCV window")
    print("python 2 - display with matplotlib")
    print("python 3 - display with Tkinter")

if len(sys.argv) != 2:
opt = sys.argv[1]

if opt=="1":
    print("display wiyh OpenCV window")
elif opt=="2":
    print("display with matplotlib")
elif opt=="3":
    print("display with Tkinter")

To use ImageTk in your python, refer to "Install PIL (with jpg supported) and ImageTk on Raspberry Pi/Raspbian".


Unknown said...

Thank you, this is good! I've have used OpenCv, but needed i more advanced GUI. I need to show a exit button, to exit the picture/window. Seems simple, but man.. It is a tough one :) Hopefully Tkinter will do the trick. Or matplotlab.. i have never heard of it. Thanks again, this gives me some work hours.

Erik said...

hello Simon Bjørkvig,

May be you can check my another post of Python to capture image from Pi Camera Module.

I have a tkButtonQuit button and quit() function.

Unknown said...

Thanks for your answer! I see you have a lot of great stuff in here. I couldnt do any overlay button with Tkinter on my cv2 image show. Have you tried working with pygame? I'm trying right now to capture a preview image with picamera, and display it with pygame. It should be interesting for your blog as well. Thanks again, keep blogging.

Erik said...

Simon Bjørkvig,

Sorry, I haven't tried pygame.

Thiago said...

Initially I would like to congratulate you for the help you have given to python users. have a question. My question is: I have two tkinter windows. One main and one secondary. The secondary window is called by a menu bar.
When second window is called, it is shown in the video captured by the picamera. I can not view the video.