Mercurial > ift6266
comparison data_generation/transformations/gimp_script.py @ 167:1f5937e9e530
More moves - transformations into data_generation, added "deep" folder
author | Dumitru Erhan <dumitru.erhan@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 14:15:38 -0500 |
parents | transformations/gimp_script.py@7f5133e1fd23 |
children | 39421555993f |
comparison
equal
deleted
inserted
replaced
166:17ae5a1a4dd1 | 167:1f5937e9e530 |
---|---|
1 #!/usr/bin/env python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 Filtres GIMP sous Python | |
6 Auteur: Nicolas Boulanger-Lewandowski | |
7 Date: Hiver 2010 | |
8 | |
9 run with: gimp -i --batch-interpreter python-fu-eval --batch - < gimp_script.py | |
10 end with: pdb.gimp_quit(0) | |
11 | |
12 Implémente le motionblur et le pinch | |
13 ''' | |
14 | |
15 from gimpfu import * | |
16 import numpy | |
17 | |
18 img = gimp.Image(32, 32, GRAY) | |
19 img.disable_undo() | |
20 layer1 = gimp.Layer(img, "layer1", 32, 32, GRAY_IMAGE, 100, NORMAL_MODE) | |
21 img.add_layer(layer1, 0) | |
22 dest_rgn = layer1.get_pixel_rgn(0, 0, 32, 32, True) | |
23 | |
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] | |
45 | |
46 def transform_image(self, image): | |
47 if self.mblur_length or self.pinch: | |
48 setpix(image) | |
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() | |
54 | |
55 return image | |
56 | |
57 # test | |
58 if __name__ == '__main__': | |
59 import Image | |
60 im = numpy.asarray(Image.open("a.bmp").convert("L")) / 255.0 | |
61 | |
62 test = GIMP1() | |
63 print test.get_settings_names(), '=', test.regenerate_parameters(1) | |
64 #for i in range(1000): | |
65 im = test.transform_image(im) | |
66 | |
67 import pylab | |
68 pylab.imshow(im, pylab.matplotlib.cm.Greys_r) | |
69 pylab.show() | |
70 | |
71 pdb.gimp_quit(0) |