Tuesday, September 22, 2015

Raspberry Pi + Python/OpenCV, show image in window

To creates window in Python/OpenCV, call:
cv2.namedWindow('imageWindow', cv2.WINDOW_AUTOSIZE)

Here is a Python example using OpenCV to load image from internet, display in window.
import sys
import cv2
import numpy
import urllib

print("Python version: \n" + sys.version)
print("cv2 version: " + cv2.__version__)
print("numpy version: " + numpy.__version__)
print("urllib version: " + urllib.__version__)

url = 'http://goo.gl/41cgQr'
data = urllib.urlopen(url)
img = numpy.asarray(bytearray(data.read()), dtype='uint8')
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
cv2.namedWindow('imageWindow', cv2.WINDOW_AUTOSIZE)
cv2.imshow('imageWindow', img)

No comments: