testinggtk r371 - trunk/tests



Author: bjornl
Date: Mon Aug 18 21:19:27 2008
New Revision: 371
URL: http://svn.gnome.org/viewvc/testinggtk?rev=371&view=rev

Log:
New utility function for counting pixels in a pixbuf

Modified:
   trunk/tests/utils.py

Modified: trunk/tests/utils.py
==============================================================================
--- trunk/tests/utils.py	(original)
+++ trunk/tests/utils.py	Mon Aug 18 21:19:27 2008
@@ -2,6 +2,7 @@
 Useful utils usable under unit testing. 
 '''
 import gtk
+from gtk import gdk
 import StringIO
 import sys
 import time
@@ -142,3 +143,32 @@
         return
     for child in widget.get_children():
         gtk_container_print_tree(child, indent + 2)
+
+def pixbuf_count_pixels(pixbuf, rgb):
+    '''
+    Returns the number of times the pixel `rgb` is present in the
+    pixbuf.
+
+    :param pixbuf: a ``gtk.gdk.Pixbuf``
+    :param rgb: a color specification string such as "#ff0000"
+    :return: the number of times `rgb` appears in the pixbuf
+    '''
+    # not handling transparent pixbufs yet
+    if pixbuf.get_has_alpha():
+        raise ValueError('pixbuf must not have alpha')
+    col = gdk.color_parse(rgb)
+
+    col.red /= 256
+    col.green /= 256
+    col.blue /= 256
+    red = chr(col.red)
+    green = chr(col.green)
+    blue = chr(col.blue)
+
+    # Abusing an iterator...
+    count = 0
+    pixels = iter(pixbuf.get_pixels())
+    for r, g, b in zip(pixels, pixels, pixels):
+        if r == red and g == green and b == blue:
+            count += 1
+    return count



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