Python Game Programming By Example

Python Game Programming By Example

Python Game Programming By Example
About the Authors
About the Reviewers

Converting OpenCV images for wxPython

As we have seen earlier, OpenCV treats images as NumPy arrays—typically, 3D arrays in BGR format or 2D arrays in grayscale format. Conversely, wxPython has its own classes for representing images, typically in RGB format (the reverse of BGR). These classes include wx.Image (an editable image), wx.Bitmap (a displayable image), and wx.StaticBitmap (a GUI element that displays a Bitmap).

Our wxUtils module will provide a function that converts a NumPy array from either BGR or grayscale to an RGB Bitmap, ready for display in a wxPython GUI. This functionality depends on OpenCV and wxPython, as reflected in the following import statements:

from CVBackwardCompat import cv2
import wx

Conveniently, wxPython provides a factory function called wx.BitmapFromBuffer(width, height, dataBuffer), which returns a new Bitmap. This function can accept a NumPy array in RGB format as the dataBuffer argument. However, a bug causes BitmapFromBuffer to fail on the first-generation...

Unlock full access

Continue reading with a subscription

Packt gives you instant online access to a library of over 7,500 practical eBooks and videos, constantly updated with the latest in tech

Your notes and bookmarks