Friday, May 26, 2017

OpenGL and OpenCV with python 2.7 - part 005.

In this tutorial, I will show you how to mount OpenCV in the Windows 10 operating system with any python version.
You can use the same steps for other versions of python.
Get the wheel binary package opencv_python- from here.

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install opencv_python-
Processing c:\python27\scripts\opencv_python-
Requirement already satisfied: numpy>=1.11.1 in c:\python27\lib\site-packages (from opencv-python==
Installing collected packages: opencv-python
Successfully installed opencv-python-

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Let's test it with default source code:

>>> import cv2
>>> dir(cv2)
Now we can test this python script example with PyQt4 python module and cv2.resize function very easy.
The example loads an image with PyQt4 python module.
from PyQt4.QtGui import QApplication, QWidget, QVBoxLayout, QImage, QPixmap, QLabel, QPushButton, QFileDialog
import cv2
import sys
app = QApplication([])
window = QWidget()
layout = QVBoxLayout(window)
display = QLabel()
width = 600
height = 400
display.setMinimumSize(width, height)
button = QPushButton('Load', window)

def read_image():
    path = QFileDialog.getOpenFileName(window)
    if path:
        print str(path)
        picture = cv2.imread(str(path))
        if picture is not None:
            print width, height
            picture = cv2.resize(picture, (width, height))
            image = QImage(picture.tobytes(),  # The content of the image
                           picture.shape[1],  # The width (number of columns)
                           picture.shape[0],  # The height (number of rows)
                           QImage.Format_RGB888)  # The image is stored in 3*8-bit format


See the result for this python script:

No comments:

Post a Comment