[mousetrap/gnome3-wip: 81/240] Move Image and related functions to mousetrap.image.



commit 8d8677a0c368a4def0b4309896f60ae493b96fcf
Author: Stoney Jackson <dr stoney gmail com>
Date:   Thu Jun 12 11:08:25 2014 -0400

    Move Image and related functions to mousetrap.image.

 src/mousetrap/gui.py    |   47 ++++---------------------------
 src/mousetrap/image.py  |   70 +++++++++++++++++++++++++++++++++++++++++++++++
 src/mousetrap/vision.py |   35 +----------------------
 3 files changed, 78 insertions(+), 74 deletions(-)
---
diff --git a/src/mousetrap/gui.py b/src/mousetrap/gui.py
index 8cdb04c..90f9eba 100644
--- a/src/mousetrap/gui.py
+++ b/src/mousetrap/gui.py
@@ -1,9 +1,9 @@
+'''
+All things GUI.
+'''
+
 from gi.repository import Gtk
 from gi.repository import Gdk
-from gi.repository import GdkPixbuf
-
-
-_GDK_PIXBUF_BIT_PER_SAMPLE = 8
 
 
 class ImageWindow(object):
@@ -22,47 +22,11 @@ class ImageWindow(object):
     def draw(self, image):
         '''Draw image to this window.
         '''
-        image = _get_pixbuf_from_image(image)
+        image = image.to_pixbuf()
         self._canvas.set_from_pixbuf(image)
         self._canvas.queue_draw()
 
 
-def _get_pixbuf_from_image(image):
-    if isinstance(image, GdkPixbuf.Pixbuf):
-        return image
-    return _cvimage_to_pixbuf(image.to_cv())
-
-
-def _cvimage_to_pixbuf(cvimage):
-    data = cvimage.tostring()
-    colorspace = GdkPixbuf.Colorspace.RGB
-    has_alpha_channel = False
-    width = cvimage.shape[1]
-    height = cvimage.shape[0]
-
-    # dist in bytes between row starts
-    row_stride = cvimage.strides[0]
-
-    # Function used to free the data when the pixbuf's reference count drops to
-    # zero, or None if the data should not be freed.
-    destroy_fn = None
-
-    # Closure data to pass to the destroy notification function.
-    destroy_fn_data = None
-
-    return GdkPixbuf.Pixbuf.new_from_data(
-            data,
-            colorspace, # FIXME: Need to handle grayscale.
-            has_alpha_channel,
-            _GDK_PIXBUF_BIT_PER_SAMPLE,
-            width,
-            height,
-            row_stride,
-            destroy_fn,
-            destroy_fn_data
-            )
-
-
 class Gui(object):
     def __init__(self):
         self._windows = {}
@@ -85,6 +49,7 @@ class Gui(object):
     def get_screen_height(self):
         return Gtk.Window().get_screen().get_height()
 
+
 class Pointer(object):
     def __init__(self):
         gdk_display = Gdk.Display.get_default()
diff --git a/src/mousetrap/image.py b/src/mousetrap/image.py
new file mode 100644
index 0000000..9af7a54
--- /dev/null
+++ b/src/mousetrap/image.py
@@ -0,0 +1,70 @@
+'''
+All things image manipulation.
+'''
+
+import cv2
+from gi.repository import GdkPixbuf
+
+
+_GDK_PIXBUF_BIT_PER_SAMPLE = 8
+
+
+class Image(object):
+    def __init__(self, image_cv, is_grayscale=False):
+        self._image_cv = image_cv
+        self._is_grayscale = is_grayscale
+        self._image_cv_grayscale = None
+        if self._is_grayscale:
+            self._image_cv_grayscale = self._image_cv
+
+    def to_cv(self):
+        return self._image_cv
+
+    def to_cv_grayscale(self):
+        if self._image_cv_grayscale is None:
+            self._image_cv_grayscale = _cv_rgb_to_cv_grayscale(self._image_cv)
+        return self._image_cv_grayscale
+
+    def to_pixbuf(self):
+        return _cvimage_to_pixbuf(self._image_cv)
+
+    def get_width(self):
+        return self._image_cv.shape[0]
+
+    def get_height(self):
+        return self._image_cv.shape[1]
+
+
+def _cv_rgb_to_cv_grayscale(image):
+    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+
+
+def _cvimage_to_pixbuf(cvimage):
+    data = cvimage.tostring()
+    colorspace = GdkPixbuf.Colorspace.RGB
+    has_alpha_channel = False
+    width = cvimage.shape[1]
+    height = cvimage.shape[0]
+
+    # dist in bytes between row starts
+    row_stride = cvimage.strides[0]
+
+    # Function used to free the data when the pixbuf's reference count drops to
+    # zero, or None if the data should not be freed.
+    destroy_fn = None
+
+    # Closure data to pass to the destroy notification function.
+    destroy_fn_data = None
+
+    return GdkPixbuf.Pixbuf.new_from_data(
+            data,
+            colorspace, # FIXME: Need to handle grayscale.
+            has_alpha_channel,
+            _GDK_PIXBUF_BIT_PER_SAMPLE,
+            width,
+            height,
+            row_stride,
+            destroy_fn,
+            destroy_fn_data
+            )
+
diff --git a/src/mousetrap/vision.py b/src/mousetrap/vision.py
index 5f3a51b..faa61c6 100644
--- a/src/mousetrap/vision.py
+++ b/src/mousetrap/vision.py
@@ -1,12 +1,10 @@
 '''
-All things computer vision. Isolates OpenCV from the rest of the system.
-If you see another file using OpenCV directly, it should probably be using
-this module instead.
+All things computer vision.
 '''
 
 import cv2
 import cv
-
+from mousetrap.image import Image
 
 
 class Camera(object):
@@ -42,35 +40,6 @@ class Camera(object):
         return Image(image)
 
 
-class Image(object):
-    def __init__(self, image_cv, is_grayscale=False):
-        self._image_cv = image_cv
-        self._is_grayscale = is_grayscale
-        self._image_cv_grayscale = None
-        if self._is_grayscale:
-            self._image_cv_grayscale = self._image_cv
-
-    def to_cv(self):
-        return self._image_cv
-
-    def to_cv_grayscale(self):
-        if self._image_cv_grayscale is None:
-            self._image_cv_grayscale = \
-                    self._cv_rgb_to_cv_grayscale(self._image_cv)
-        return self._image_cv_grayscale
-
-    def get_width(self):
-        return self._image_cv.shape[0]
-
-    def get_height(self):
-        return self._image_cv.shape[1]
-
-
-    @staticmethod
-    def _cv_rgb_to_cv_grayscale(image):
-        return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
-
-
 class HaarLoader(object):
     _haar_files = {
         "face": "haars/haarcascade_frontalface_default.xml",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]