Mercurial > ift6266
annotate data_generation/transformations/gimp_script.py @ 215:334d2444000d
Changes that enable using this code when floatX=float32
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Wed, 10 Mar 2010 13:48:16 -0500 |
parents | 1f5937e9e530 |
children | 39421555993f |
rev | line source |
---|---|
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
2 # coding: utf-8 |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
3 |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
4 ''' |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
5 Filtres GIMP sous Python |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
6 Auteur: Nicolas Boulanger-Lewandowski |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
7 Date: Hiver 2010 |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
8 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
9 run with: gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
10 end with: pdb.gimp_quit(0) |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
11 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
12 Implémente le motionblur et le pinch |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
13 ''' |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
14 |
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
15 from gimpfu import * |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
16 import numpy |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
17 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
18 img = gimp.Image(32, 32, GRAY) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
19 img.disable_undo() |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
20 layer1 = gimp.Layer(img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
21 img.add_layer(layer1, 0) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
22 dest_rgn = layer1.get_pixel_rgn(0, 0, 32, 32, True) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
23 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
24 def setpix(image): |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
25 dest_rgn[:,:] = (image.T*255).astype(numpy.uint8).tostring() |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
26 layer1.flush() |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
27 layer1.update(0, 0, 32, 32) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
28 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
29 def getpix(): |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
30 return numpy.fromstring(dest_rgn[:,:], 'UInt8').astype(numpy.float32).reshape((32,32)).T / 255.0 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
31 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
32 class GIMP1(): |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
33 def get_settings_names(self): |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
34 return ['mblur_length', 'mblur_angle', 'pinch'] |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
35 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
36 def regenerate_parameters(self, complexity): |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
37 if complexity: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
38 self.mblur_length = abs(int(round(numpy.random.normal(0, 3*complexity)))) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
39 else: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
40 self.mblur_length = 0 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
41 self.mblur_angle = int(round(numpy.random.uniform(0,360))) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
42 self.pinch = numpy.random.uniform(-complexity, 0.7*complexity) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
43 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
44 return [self.mblur_length, self.mblur_angle, self.pinch] |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
45 |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
46 def transform_image(self, image): |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
47 if self.mblur_length or self.pinch: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
48 setpix(image) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
49 if self.mblur_length: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
50 pdb.plug_in_mblur(img, layer1, 0, self.mblur_length, self.mblur_angle, 0, 0) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
51 if self.pinch: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
52 pdb.plug_in_whirl_pinch(img, layer1, 0.0, self.pinch, 1.0) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
53 image = getpix() |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
54 |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
55 return image |
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
56 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
57 # test |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
58 if __name__ == '__main__': |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
59 import Image |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
60 im = numpy.asarray(Image.open("a.bmp").convert("L")) / 255.0 |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
61 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
62 test = GIMP1() |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
63 print test.get_settings_names(), '=', test.regenerate_parameters(1) |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
64 #for i in range(1000): |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
65 im = test.transform_image(im) |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
66 |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
67 import pylab |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
68 pylab.imshow(im, pylab.matplotlib.cm.Greys_r) |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
69 pylab.show() |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
70 |
12
d511445f19da
appliquer filtres gimp sur des arrays numpy plutot que sur des fichiers et structure classe
boulanni <nicolas_boulanger@hotmail.com>
parents:
7
diff
changeset
|
71 pdb.gimp_quit(0) |