comparison transformations/gimp_script.py @ 45:f8a92292b299

merge de 4 fevrier
author SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
date Thu, 04 Feb 2010 10:27:58 -0500
parents 7f5133e1fd23
children
comparison
equal deleted inserted replaced
44:5deccb161307 45:f8a92292b299
4 ''' 4 '''
5 Filtres GIMP sous Python 5 Filtres GIMP sous Python
6 Auteur: Nicolas Boulanger-Lewandowski 6 Auteur: Nicolas Boulanger-Lewandowski
7 Date: Hiver 2010 7 Date: Hiver 2010
8 8
9 run with: 9 run with: gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py
10 gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py 10 end with: pdb.gimp_quit(0)
11 11
12 inclure pour terminer gimp à la fin du script: 12 Implémente le motionblur et le pinch
13 pdb.gimp_quit(0)
14
15 Décommenter les lignes appropriées pour différents filtres
16 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).
17 Les autres fonctions du programme sont dans la Procedure DataBase (PDB) dans le menu Help > Procedure Browser.
18 ''' 13 '''
19 14
20 from gimpfu import * 15 from gimpfu import *
21 import numpy 16 import numpy
22 17
23 class GIMPTransformation(): 18 img = gimp.Image(32, 32, GRAY)
24 def __init__(self): 19 img.disable_undo()
25 self.img = gimp.Image(32, 32, GRAY) 20 layer1 = gimp.Layer(img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE)
26 self.img.disable_undo() 21 img.add_layer(layer1, 0)
27 self.layer1 = gimp.Layer(self.img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE) 22 dest_rgn = layer1.get_pixel_rgn(0, 0, 32, 32, True)
28 self.img.add_layer(self.layer1, 0) 23
29 self.dest_rgn = self.layer1.get_pixel_rgn(0, 0, 32, 32, True) 24 def setpix(image):
25 dest_rgn[:,:] = (image.T*255).astype(numpy.uint8).tostring()
26 layer1.flush()
27 layer1.update(0, 0, 32, 32)
28
29 def getpix():
30 return numpy.fromstring(dest_rgn[:,:], 'UInt8').astype(numpy.float32).reshape((32,32)).T / 255.0
31
32 class GIMP1():
33 def get_settings_names(self):
34 return ['mblur_length', 'mblur_angle', 'pinch']
35
36 def regenerate_parameters(self, complexity):
37 if complexity:
38 self.mblur_length = abs(int(round(numpy.random.normal(0, 3*complexity))))
39 else:
40 self.mblur_length = 0
41 self.mblur_angle = int(round(numpy.random.uniform(0,360)))
42 self.pinch = numpy.random.uniform(-complexity, 0.7*complexity)
43
44 return [self.mblur_length, self.mblur_angle, self.pinch]
30 45
31 def transform_image(self, image): 46 def transform_image(self, image):
32 ''' si la ligne suivante échoue, upgrader à GIMP v.2.6.7 47 if self.mblur_length or self.pinch:
33 OU utiliser une des 2 versions commentées ci-après (10x plus lent) ''' 48 setpix(image)
34 self.dest_rgn[:,:] = (image.T*255).astype(numpy.uint8).tostring() 49 if self.mblur_length:
50 pdb.plug_in_mblur(img, layer1, 0, self.mblur_length, self.mblur_angle, 0, 0)
51 if self.pinch:
52 pdb.plug_in_whirl_pinch(img, layer1, 0.0, self.pinch, 1.0)
53 image = getpix()
35 54
36 #for i in range(32):
37 # self.dest_rgn[i,:] = (image[i,:]*255).astype(numpy.uint8).tostring()
38
39 #for i in range(32):
40 # for j in range(32):
41 # self.dest_rgn[i,j] = chr(int((image[i,j] *255)))
42
43 self.layer1.flush()
44 self.layer1.update(0, 0, 32, 32)
45
46 ''' application des filtres '''
47 #pdb.plug_in_noisify(self.img, self.layer1, 0, 0.4, 0, 0, 0)
48 #pdb.plug_in_c_astretch(self.img, self.layer1)
49 #pdb.plug_in_emboss(self.img, self.layer1, 10, 30, 5, 0)
50 #pdb.plug_in_applylens(self.img, self.layer1, 2, 1, 0, 0)
51 #pdb.plug_in_blur(self.img, self.layer1)
52 pdb.plug_in_gauss_rle(self.img, self.layer1, 9, 1, 0)
53
54 ''' si la ligne suivante échoue, upgrader à GIMP v.2.6.7
55 OU utiliser la version commentée ci-après (10x plus lent) '''
56 image = numpy.fromstring(self.dest_rgn[:,:], 'UInt8').reshape((32,32)).T / 255.0
57
58 #image = image*0
59 #for i in range(32):
60 # for j in range(32):
61 # image[i,j] = ord(self.dest_rgn[i,j]) /255.0
62
63 return image 55 return image
64 56
65 # TEST 57 # test
66 if 1: 58 if __name__ == '__main__':
67 im = numpy.zeros((32,32)) 59 import Image
68 for i in range(32): 60 im = numpy.asarray(Image.open("a.bmp").convert("L")) / 255.0
69 for j in range(32):
70 im[i,j] = i*j/(32.*32.)
71 61
72 test = GIMPTransformation() 62 test = GIMP1()
73 #for i in range(100): 63 print test.get_settings_names(), '=', test.regenerate_parameters(1)
64 #for i in range(1000):
74 im = test.transform_image(im) 65 im = test.transform_image(im)
75 66
76 import pylab 67 import pylab
77 pylab.imshow(im) 68 pylab.imshow(im, pylab.matplotlib.cm.Greys_r)
78 pylab.show() 69 pylab.show()
79 70
80 pdb.gimp_quit(0) 71 pdb.gimp_quit(0)