diff transformations/gimp_script.py @ 42:7f5133e1fd23

GIMP filtres de motion blur et de pinch
author boulanni <nicolas_boulanger@hotmail.com>
date Wed, 03 Feb 2010 20:06:45 -0500
parents d511445f19da
children
line wrap: on
line diff
--- a/transformations/gimp_script.py	Wed Feb 03 17:08:27 2010 -0500
+++ b/transformations/gimp_script.py	Wed Feb 03 20:06:45 2010 -0500
@@ -6,75 +6,66 @@
 Auteur: Nicolas Boulanger-Lewandowski
 Date: Hiver 2010
 
-run with:
-gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py
+run with: gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py
+end with: pdb.gimp_quit(0)
 
-inclure pour terminer gimp à la fin du script:
-pdb.gimp_quit(0)
-
-Décommenter les lignes appropriées pour différents filtres
-Les plug-ins de GIMP et leurs paramètres sont disponibles sous GIMP, menu Help > Plug-in Browser (toujours ignorer le paramètre run-mode).
-Les autres fonctions du programme sont dans la Procedure DataBase (PDB) dans le menu Help > Procedure Browser.
+Implémente le motionblur et le pinch
 '''
 
 from gimpfu import *
 import numpy
 
-class GIMPTransformation():
-    def __init__(self):
-        self.img = gimp.Image(32, 32, GRAY)
-        self.img.disable_undo()
-        self.layer1 = gimp.Layer(self.img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE)
-        self.img.add_layer(self.layer1, 0)
-        self.dest_rgn = self.layer1.get_pixel_rgn(0, 0, 32, 32, True)
+img = gimp.Image(32, 32, GRAY)
+img.disable_undo()
+layer1 = gimp.Layer(img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE)
+img.add_layer(layer1, 0)
+dest_rgn = layer1.get_pixel_rgn(0, 0, 32, 32, True)
+
+def setpix(image):
+    dest_rgn[:,:] = (image.T*255).astype(numpy.uint8).tostring()
+    layer1.flush()
+    layer1.update(0, 0, 32, 32)
+
+def getpix():
+    return numpy.fromstring(dest_rgn[:,:], 'UInt8').astype(numpy.float32).reshape((32,32)).T / 255.0
+
+class GIMP1():
+    def get_settings_names(self):
+        return ['mblur_length', 'mblur_angle', 'pinch']
+    
+    def regenerate_parameters(self, complexity):
+        if complexity:
+            self.mblur_length = abs(int(round(numpy.random.normal(0, 3*complexity))))
+        else:
+            self.mblur_length = 0
+        self.mblur_angle =  int(round(numpy.random.uniform(0,360)))
+        self.pinch = numpy.random.uniform(-complexity, 0.7*complexity)
+
+        return [self.mblur_length, self.mblur_angle, self.pinch]
 
     def transform_image(self, image):
-        ''' si la ligne suivante échoue, upgrader à GIMP v.2.6.7
-        OU utiliser une des 2 versions commentées ci-après (10x plus lent) '''
-        self.dest_rgn[:,:] = (image.T*255).astype(numpy.uint8).tostring()
-
-        #for i in range(32):
-        # self.dest_rgn[i,:] = (image[i,:]*255).astype(numpy.uint8).tostring()
-
-        #for i in range(32):
-        # for j in range(32):
-        #  self.dest_rgn[i,j] = chr(int((image[i,j] *255)))
-
-        self.layer1.flush()
-        self.layer1.update(0, 0, 32, 32)
+        if self.mblur_length or self.pinch:
+            setpix(image)
+            if self.mblur_length:
+                pdb.plug_in_mblur(img, layer1, 0, self.mblur_length, self.mblur_angle, 0, 0)
+            if self.pinch:        
+                pdb.plug_in_whirl_pinch(img, layer1, 0.0, self.pinch, 1.0)
+            image = getpix()
 
-        ''' application des filtres '''
-        #pdb.plug_in_noisify(self.img, self.layer1, 0, 0.4, 0, 0, 0)
-        #pdb.plug_in_c_astretch(self.img, self.layer1)
-        #pdb.plug_in_emboss(self.img, self.layer1, 10, 30, 5, 0)
-        #pdb.plug_in_applylens(self.img, self.layer1, 2, 1, 0, 0)
-        #pdb.plug_in_blur(self.img, self.layer1)
-        pdb.plug_in_gauss_rle(self.img, self.layer1, 9, 1, 0)
-
-        ''' si la ligne suivante échoue, upgrader à GIMP v.2.6.7
-        OU utiliser la version commentée ci-après (10x plus lent) '''
-        image = numpy.fromstring(self.dest_rgn[:,:], 'UInt8').reshape((32,32)).T / 255.0
-
-        #image = image*0
-        #for i in range(32):
-        # for j in range(32):
-        #  image[i,j] = ord(self.dest_rgn[i,j]) /255.0
-        
         return image
 
-# TEST
-if 1:
-    im = numpy.zeros((32,32))
-    for i in range(32):
-     for j in range(32):
-      im[i,j] = i*j/(32.*32.)
+# test
+if __name__ == '__main__':
+    import Image
+    im = numpy.asarray(Image.open("a.bmp").convert("L")) / 255.0
 
-    test = GIMPTransformation()
-    #for i in range(100):
+    test = GIMP1()
+    print test.get_settings_names(), '=', test.regenerate_parameters(1)
+    #for i in range(1000):
     im = test.transform_image(im)
-    
+
     import pylab
-    pylab.imshow(im)
+    pylab.imshow(im, pylab.matplotlib.cm.Greys_r)
     pylab.show()
 
     pdb.gimp_quit(0)