Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Game Programming By Example

You're reading from   Python Game Programming By Example A pragmatic guide for developing your own games with Python

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785281532
Length 230 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime