Mercurial > ift6266
annotate data_generation/transformations/gimp_script.py @ 266:1e4e60ddadb1
Merge. Ah, et dans le dernier commit, j'avais oublié de mentionner que j'ai ajouté du code pour gérer l'isolation de différents clones pour rouler des expériences et modifier le code en même temps.
author | fsavard |
---|---|
date | Fri, 19 Mar 2010 10:56:16 -0400 |
parents | dd2df78fcf47 |
children | d5b2b6397a5a |
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(): |
244
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
33 def __init__(self, blur_bool = True): |
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
34 #This is used to avoid blurring for PNIST |
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
35 self.blur_bool = blur_bool |
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
36 |
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
37 def get_settings_names(self, blur_bool = True): |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
38 return ['mblur_length', 'mblur_angle', 'pinch'] |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
39 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
40 def regenerate_parameters(self, complexity): |
254
dd2df78fcf47
added option to pipeline and gimp_script to produce NIST-friendly data
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
244
diff
changeset
|
41 if complexity: |
244
39421555993f
small change in gimp_script to avoid blurring in PNIST
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
167
diff
changeset
|
42 self.mblur_length = abs(int(round(numpy.random.normal(0, 3*complexity)))) |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
43 else: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
44 self.mblur_length = 0 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
45 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
|
46 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
|
47 |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
48 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
|
49 |
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
|
50 def transform_image(self, image): |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
51 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
|
52 setpix(image) |
254
dd2df78fcf47
added option to pipeline and gimp_script to produce NIST-friendly data
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents:
244
diff
changeset
|
53 if self.mblur_length and self.blur_bool: |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
54 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
|
55 if self.pinch: |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
56 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
|
57 image = getpix() |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
58 |
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
|
59 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
|
60 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
61 # test |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
62 if __name__ == '__main__': |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
63 import Image |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
64 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
|
65 |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
66 test = GIMP1() |
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
67 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
|
68 #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
|
69 im = test.transform_image(im) |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
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 import pylab |
42
7f5133e1fd23
GIMP filtres de motion blur et de pinch
boulanni <nicolas_boulanger@hotmail.com>
parents:
12
diff
changeset
|
72 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
|
73 pylab.show() |
7
f2d46bb3f2d5
Ajout de filtres GIMP (transformations/gimp_script.py)
boulanni <nicolas_boulanger@hotmail.com>
parents:
diff
changeset
|
74 |
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
|
75 pdb.gimp_quit(0) |