gimp r26010 - in branches/soc-2008-python: . plug-ins/pygimp



Author: larsc
Date: Sat Jun 28 16:24:58 2008
New Revision: 26010
URL: http://svn.gnome.org/viewvc/gimp?rev=26010&view=rev

Log:
2008-08-16  Lars-Peter Clausen  <lars metafoo de>

	* plug-ins/pygimp/pygimp-pdb.c: Fix typo
	* plug-ins/pygimp/__init__.py: pixel propertys now return a tuple of
	gimp.Color. Fixed gradient range index issues.
	* plug-ins/pygimp/Makefile: Added tools.py
	* plug-ins/pygimp/tools.py: New module that provides functions for
	paint and select tools


Added:
   branches/soc-2008-python/plug-ins/pygimp/tools.py
Modified:
   branches/soc-2008-python/ChangeLog
   branches/soc-2008-python/plug-ins/pygimp/Makefile.am
   branches/soc-2008-python/plug-ins/pygimp/__init__.py
   branches/soc-2008-python/plug-ins/pygimp/pygimp-pdb.c

Modified: branches/soc-2008-python/plug-ins/pygimp/Makefile.am
==============================================================================
--- branches/soc-2008-python/plug-ins/pygimp/Makefile.am	(original)
+++ branches/soc-2008-python/plug-ins/pygimp/Makefile.am	Sat Jun 28 16:24:58 2008
@@ -131,7 +131,8 @@
 	context.py	\
 	ui.py	\
 	shelf.py	\
-	enums.py
+	enums.py	\
+	tools.py
 
 codegen_files = \
 	gimpcolor-types.defs	\

Modified: branches/soc-2008-python/plug-ins/pygimp/__init__.py
==============================================================================
--- branches/soc-2008-python/plug-ins/pygimp/__init__.py	(original)
+++ branches/soc-2008-python/plug-ins/pygimp/__init__.py	Sat Jun 28 16:24:58 2008
@@ -1,4 +1,4 @@
-# -*- Mode: Python; py-indent-offset: 3 -*-
+# -*- Mode: Python; py-indent-offset: 4 -*-
 # Gimp-Python - allows the writing of Gimp plugins in Python.
 # Copyright (C) 2008 Lars-Peter Clausen <lars metafoo de>
 #
@@ -36,6 +36,10 @@
     """Helper function for creating properties"""
     return property(doc=func.__doc__, *func())
 
+def _pack_pixels(pixel, bpp):
+    """Packs a tuple of pixel data into a tuple of bpp sized tuples"""
+    return zip(*(iter(pixel),)*bpp)
+
 class GimpNamedObject(object):
     """
     GimpNamedObject is a bases class for wrappers around gimp objetcs.
@@ -141,14 +145,16 @@
     mask_bpp = property(fget=lambda self:pdb.gimp_brush_get_info(self)[2],
                         doc="""The number of bits per pixel of the brushes mask
                              property.""")
-    mask = property(fget=lambda self:pdb.gimp_brush_get_pixels(self)[4],
-                    doc="""The brushes mask. A height-tuple of width-tuples""")
+    mask = property(fget=lambda self: 
+               _pack_pixels(pdb.gimp_brush_get_pixels(self)[4], self.mask_bpp),
+               doc="""The brushes mask. A height-tuple of width-tuples""")
     pixel_bpp = property(fget=lambda self:pdb.gimp_brush_get_info(self)[3],
                         doc="""The number of bits per pixel of the brushes pixel
                              property.""")
-    pixel = property(fget=lambda self:pdb.gimp_brush_get_pixels(self)[7],
-                    doc="""The brushes pixel data. A height-tuple of
-                           width-tuples""")
+    pixel = property(fget=lambda self:
+              _pack_pixels(pdb.gimp_brush_get_pixels(self)[7], self.pixel_bpp),
+              doc="""The brushes pixel data. A height-tuple of
+                     width-tuples""")
     spacing = property(fget=lambda self:pdb.gimp_brush_get_spacing(self),
                    fset=lambda self, val:pdb.gimp_brush_set_spacing(self, val),
                    doc="""The brushes spacing. Valid values are 0 <= spacing <=
@@ -172,7 +178,7 @@
                      fset=lambda self, val:pdb.gimp_brush_set_angle(self, val),
                      doc="""The brushes rotation angle in degrees.""")
     aspect_ratio = property(
-                    fget=lambda self:pdb.gimp_brush_get_aspect_ratio(self),
+               fget=lambda self:pdb.gimp_brush_get_aspect_ratio(self),
                fset=lambda self,val:pdb.gimp_brush_set_aspect_ratio(self, val),
                doc="""The brushes aspect ratio.""")
     hardness = property(fget=lambda self:pdb.gimp_brush_get_hardness(self),
@@ -204,9 +210,10 @@
     doc="""Height of the pattern in pixels""")
     pixel_bpp = property(fget=lambda self:pdb.gimp_pattern_get_info(self)[2],
     doc="""Bytes per pixel of the pixels attribute""")
-    pixels = property(fget=lambda self:pdb.gimp_pattern_get_pixels(self)[4],
-    doc="""A tupel containing the pixel values for the pattern. It length is
-    bpp*widht*height.""")
+    pixels = property(fget=lambda self:
+             _pack_pixels(pdb.gimp_pattern_get_pixels(self)[4], self.pixel_bpp),
+             doc="""A tupel containing the pixel values for the pattern. It length is
+        bpp*widht*height.""")
 
 class Palette(GimpNamedObject):
     """A gimp Palette object. A Palette instance provides a list like interface
@@ -381,11 +388,10 @@
            
         samples = pdb.gimp_gradient_get_uniform_samples(self, num_samples,
                                                         reverse)[1]
-        return map(color.RGB,*(iter(samples),)*4)
+        return map(color.RGB, *(iter(samples),)*4)
 
     segments = property(
-        fget=lambda self: GradientSegmentRange(self, 0, 
-                         pdb.gimp_gradient_get_number_of_segments(self) - 1),
+        fget=lambda self: GradientSegmentRange(self, 0, -1),
         fdel=lambda self:pdb.gimp_gradient_segment_range_delete(self, 0, -1),
         doc="""A gimp.GradientSegmentRange used to access the segemnts of the
                gradient.""")
@@ -501,10 +507,15 @@
         self.end = end
 
     def __repr__(self):
+        end = self.end + 1
+        if end < 0:
+            end = pdb.gimp_gradient_get_number_of_segments(self.gradient)
         return "<gimp.GradientSegmentRange (%s, %d, %d)>"% (str(self.gradient),
-                                                          self.start, self.end)
+                                                          self.start, end)
     
     def __len__(self):
+        if self.end < 0:
+            return pdb.gimp_gradient_get_number_of_segments(self.gradient) - self.start
         return self.end - self.start + 1
 
     def __getitem__(self, key):

Modified: branches/soc-2008-python/plug-ins/pygimp/pygimp-pdb.c
==============================================================================
--- branches/soc-2008-python/plug-ins/pygimp/pygimp-pdb.c	(original)
+++ branches/soc-2008-python/plug-ins/pygimp/pygimp-pdb.c	Sat Jun 28 16:24:58 2008
@@ -320,20 +320,20 @@
 static void
 pygimp_set_type_error(int n, gchar *expected, PyObject *got, const gchar *error_prefix)
 {
-    static const gchar *positions[] = {"first", "second"};
+    static const gchar *positions[] = {"first", "second", "third"};
     
     if(n < 1)
         PyErr_Format(PyExc_TypeError, "%sExpected %s, got %s "
                      "instead.", error_prefix ? error_prefix : "", expected,
                      got->ob_type->tp_name);
-     if(n < 3)
-        PyErr_Format(PyExc_TypeError, "%sExpected %s as %s parameter, got %s "
-                     "instead.", error_prefix ? error_prefix : "", expected,
-                     positions[n-1], got->ob_type->tp_name);
+     if(n < 4)
+        PyErr_Format(PyExc_TypeError, "%s%s argument must be %s, not %s.",
+                     error_prefix ? error_prefix : "", positions[n-1],
+                     expected, got->ob_type->tp_name);
     else
-        PyErr_Format(PyExc_TypeError, "%sExpected %s as %drd parameter, got %s "
-                     "instead.", error_prefix ? error_prefix : "", expected,
-                     n + 1, got->ob_type->tp_name);
+        PyErr_Format(PyExc_TypeError, "%s%dth argument must be %s, not %s.",
+                     error_prefix ? error_prefix : "", n, expected,
+                     got->ob_type->tp_name);
 }
 
 GimpParam *
@@ -780,7 +780,7 @@
 
     if (!gimp_procedural_db_proc_info (name, &b, &h, &a, &c, &d, &pt,
 				       &np, &nr, &p, &r)) {
-	PyErr_SetString(pygimp_error, "procedure not found");
+	PyErr_Format(pygimp_error, "%s: Procedure not found", name);
 	return NULL;
     }
 

Added: branches/soc-2008-python/plug-ins/pygimp/tools.py
==============================================================================
--- (empty file)
+++ branches/soc-2008-python/plug-ins/pygimp/tools.py	Sat Jun 28 16:24:58 2008
@@ -0,0 +1,224 @@
+# *- Mode: Python; py-indent-offset: 4 -*-
+# Gimp-Python - allows the writing of Gimp plugins in Python.
+# Copyright (C) 2008 Lars-Peter Clausen <lars metafoo de>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+
+"""This module provides functions for painting and selecting regions on an image
+   or drawable."""
+
+import gimp
+
+# Helper functions
+
+def _active_image():
+    """Returns the active image as gimp.Image or None."""
+    il = gimp.image_list()
+    if(len(il) > 0):
+        return il[0]
+    return None
+
+def _active_drawable():
+    """Returns the active drawable of the active image as gimp.Drawable or 
+       None."""
+    active_image = _active_image()
+    if active_image:
+        return active_image.active_drawable
+    return None
+
+# Paint tools
+
+def brush(drawable, first_point, *args):
+    if drawable == None:
+        drawable = _active_drawable()
+    points = [first_point] + list(args)
+    points = sum(map(list, points), [])
+    gimp.pdb.gimp_brush(drawable, len(points), points)
+    
+def clone(drawable, strokes, src_drawable = None, src_x = 0, src_y = 0,
+          clone_type = gimp.enums.IMAGE_CLONE):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    if not src_drawable:
+        src_drawable = drawable
+
+    gimp.pdb.gimp_clone(drawable, src_drawable, clone_type, src_x, src_y,
+                        len(strokes), strokes)
+
+def convolve(drawable, strokes, pressure = 50,
+             convolve_type = gimp.enums.CONVOLVE_BLUR):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    gimp.pdb.gimp_convolve(drawable, pressure, convolve_type, len(strokes), strokes)
+
+def dodgeburn(drawable, strokes, exposure = 50, type = gimp.enums.DODGE,
+              mode = gimp.enums.SHADOWS):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    gimp.pdb.gimp_dodgeburn(drawable, exposure, type, mode, len(strokes),
+                            strokes)
+
+def eraser(drawable, strokes, soft = False, method = gimp.enums.PAINT_CONSTANT):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    gimp.pdb.gimp_eraser(drawable, len(strokes), strokes, soft, method)
+
+def paintbrush(drawable, strokes, fade_out = 0, 
+               method = gimp.enums.PAINT_CONSTANT, gradient_length = 0):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    gimp.pdb.gimp_paintbrush(drawable, fade_out, len(strokes), strokes, method,
+                             gradient_length)
+
+def pencil(drawable, first_point, *args):
+    if drawable == None:
+        drawable = _active_drawable()
+    points = [first_point] + list(args)
+    points = sum(map(list, points), [])
+    gimp.pdb.gimp_pencil(drawable, len(points), points)
+
+def smudge(drawable, strokes, pressure = 50):
+    if drawable == None:
+        drawable = _active_drawable()
+    strokes = sum(map(list, strokes), [])
+    gimp.pdb.gimp_smudge(drawable, pressure, len(strokes), strokes)
+
+# Edit tools
+
+def blend(drawable, blend_mode = gimp.enums.FG_BG_RGB_MODE, 
+          paint_mode = gimp.enums.NORMAL_MODE,
+          gradient_type = gimp.enums.GRADIENT_LINEAR,
+          opacity = 100, offset = 0, repeat_mode = gimp.enums.REPEAT_NONE,
+          reverse = False, supersample = False, max_supersampling_depth = 1,
+          supersampling_threshold = 1, dither = False, x1 = 0, y1 = 0, x2 = 0,
+          y2 = 0):
+    if drawable == None:
+        drawable = _active_drawable()
+    gimp.pdb.gimp_edit_blend(drawable, blend_mode, paint_mode, gradient_type,
+                             opacity, offset, repeat_mode, reverse, supersample,
+                             max_supersampling_depth, supersampling_threshold,
+                             dither, x1, y1, x2, y2)
+
+def fill(drawable, fill_mode = gimp.enums.FOREGROUND_FILL):
+    if drawable == None:
+        drawable = _active_drawable()
+    gimp.pdb.gimp_edit_fill(drawable, fill_mode)
+
+def bucket_fill(drawable, fill_mode = gimp.enums.FG_BUCKET_FILL,
+                paint_mode = gimp.enums.NORMAL_MODE, opacity = 100,
+                threshold = 0, sample_merged = False, fill_transparent = False,
+                select_criterion = gimp.enums.SELECT_CRITERION_COMPOSITE,
+                x = 0, y = 0):
+    if drawable == None:
+        drawable = _active_drawable()
+    gimp.pdb.gimp_edit_bucket_fill_full(drawable, fill_mode, paint_mode,
+                                        opacity, threshold, sample_merged,
+                                        fill_transparent, select_criterion,
+                                        x, y)
+
+def stroke_selection(drawable):
+    if drawable == None:
+        drawable = _active_drawable()
+    gimp.pdb.gimp_edit_stroke(drawable)
+    
+def stroke_vectors(drawable, vectors):
+    if drawable == None:
+        drawable = _active_drawable()
+    gimp.pdb.gimp_edit_stroke_vectors(drawable, vectors)
+
+# Select tools
+
+def _feather_helper(feather_radius):
+    if feather_radius == None:
+        return False, 0.0
+    return True, feather_radius
+
+def _radius_helper(radius):
+    if isinstance(radius, tuple):
+        return radius
+    return radius, radius
+
+def select_by_color(drawable, color, threshold = 0,
+                    operation = gimp.enums.CHANNEL_OP_REPLACE,
+                    antialias = False, feather_radius = 0,
+                    sample_merged = False, select_transparent = False,
+                    select_criterion = gimp.enums.SELECT_CRITERION_COMPOSITE):
+    if drawable == None:
+        drawable = _active_drawable()
+    do_feather, feather_radius = _feather_helper(feater_radius)
+    feather_radius_x, feather_radius_y = _radius_helper(feather_radius)
+    
+    gimp.pdb.gimp_by_color_select(drawable, color, threshold, operation,
+                                  antialias, do_feather, feather_radius_x,
+                                  feather_radius_y, sample_merged,
+                                  select_transparent, select_criterion)
+
+def select_elipse(image, x, y, width, height,
+                  operation = gimp.enums.CHANNEL_OP_REPLACE,
+                  feather_radius = None):
+    do_feather, feather_radius = _feather_helper(feater_radius)
+    
+    gimp.pdb.gimp_elipse_select(image, x, y, width, height, operation,
+                                do_feather, feather_radius)
+
+def select_free(image, points, operation = gimp.enums.CHANNEL_OP_REPLACE,
+                antialias = False, feather_radius = None):
+    do_feather, feather_radius = _feather_helper(feater_radius)
+    points = sum(map(list, points), [])
+   
+    gimp.pdb.free_select(image, points, len(points), operation, antialias,
+                         do_feather, feather_radius)
+
+def select_fuzzy(drawable, x, y, threshold = 0,
+                 operation = gimp.enums.CHANNEL_OP_REPLACE, antialias = False,
+                 feather_radius = None, sample_merged = False,
+                 select_transparent = False,
+                 select_criterion = gimp.enums.SELECT_CRITERION_COMPOSITE):
+    if drawable == None:
+        drawable = _active_drawable()
+    do_feather, feather_radius = _feather_helper(feater_radius)
+    
+    gimp.pdb.fuzzy_select(image, x, y, threshold, antialias, do_feather,
+                          feather_radius, sample_merged, select_transparent,
+                          select_criterion)
+
+def select_rect(image, x, y, width, height,
+                operation = gimp.enums.CHANNEL_OP_REPLACE,
+                feather_radius = None):
+    do_feather, feather_radius = _feather_helper(feater_radius)
+    
+    gimp.pdb.gimp_rect_select(image, x, y, width, height, operation,
+                              do_feather, feather_radius)
+
+def select_round_rect(image, x, y, width, height, corner_radius,
+                      operation = gimp.enums.CHANNEL_OP_REPLACE,
+                      antialias = False, feather_radius = None):
+    do_feather, feather_radius = _feather_helper(feather_radius)
+    feather_radius_x, feather_radius_y = _radius_helper(feather_radius)
+    corner_radius_x, corner_radius_y = _radius_helper(corner_radius)
+
+    gimp.pdb.gimp_round_rect_select(image, x, y, width, height, corner_radius_x,
+                                    corner_radius_y, operation, antialias,
+                                    do_feather, feather_radius_x,
+                                    feather_radius_y)
+
+
+    



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