1
2
3
4
5
6
7
8
9
10
11
12 from NetworkEditor.items import NetworkNode
13 from magick import *
14
15
17 args = "image("
18 for i in range(len(imgList)):
19 args = args + "imgList[" + str(i) + "], "
20 args = args + ")"
21 result = eval(args)
22 return result
23
24
25
26 -class Read(NetworkNode):
27 """Reads an image from a file.
28
29 File Name: the file to be read. Double click for file browser."""
30
32 kw['name'] = name
33 apply(NetworkNode.__init__, (self,), kw)
34
35 code = """def doit(self, filename):
36 if filename and filename != 'File Name':
37 result = image(filename)
38 if result:
39 self.outputData(outImage=result)\n"""
40
41 self.setFunction(code)
42
43 fileTypes = [('all', '*')]
44 self.widgetDescr['filename'] = {
45 'class': 'NEEntryWithFileBrowser',
46 'master': 'node',
47 'filetypes': fileTypes,
48 'initialValue': 'File Name',
49 'title': 'Read Image',
50 'width': 10,}
51
52 self.inputPortsDescr.append({
53 'name': 'filename',
54 'datatype': 'string'})
55 self.outputPortsDescr.append({
56 'name': 'outImage',
57 'datatype': 'imimage'})
58
60 """Writes an image to a file. Please insure that if you are using the correct format if you are saving multiple images to one file.
61
62 File Name: the file to be written to. Double click for file browser."""
63
65 kw['name'] = name
66 apply(NetworkNode.__init__, (self,), kw)
67
68 code = """def doit(self, img, filename):
69 img = toSeq(img)
70 if filename and filename != 'File Name':
71 img.write(filename)\n"""
72
73 self.setFunction(code)
74
75 fileTypes = [('all', '*')]
76 self.widgetDescr['filename'] = {
77 'class': 'NEEntryWithFileSaver',
78 'master': 'node',
79 'initialValue': 'File Name',
80 'filetypes': fileTypes,
81 'title': 'Write Image',
82 'width': 10 }
83
84 self.inputPortsDescr.append({
85 'name': 'img',
86 'singleConnection':False,
87 'datatype': 'imimage'})
88 self.inputPortsDescr.append({
89 'name': 'filename',
90 'datatype': 'string'})
91
93 """Converts an image to the PIL datatype for use with the included imagelib.
94
95 Removes transparency information. Only works with RGB images. Flattens image sequences."""
96
98 kw['name'] = name
99 apply(NetworkNode.__init__, (self,), kw)
100
101 code = """def doit(self, img):
102 import Image, Numeric
103
104 if len(img) > 1:
105 img = toSeq(img)
106 img = flatten(img)
107 else:
108 img = img[0]
109
110 Aim = img.toarray()
111 Aim = Aim[:,:,0:3]
112 Aim = Aim / 257
113 h, w, c = Aim.shape
114 Aim = Numeric.reshape(Aim, (1, w*h*c))
115 Aim = Aim.astype(Numeric.UnsignedInt8)
116
117 result = Image.fromstring('RGB', (image.width,image.height), Aim.tostring())
118
119 self.outputData(outImage=result)\n"""
120
121 self.setFunction(code)
122
123 self.inputPortsDescr.append({
124 'name': 'img',
125 'singleConnection':False,
126 'datatype': 'imimage'})
127 self.outputPortsDescr.append({
128 'name': 'outImage',
129 'datatype': 'image'})
130
132 """Uses Tkinter and PIL to display an image.
133
134 Does not display transparent colors. Only works with RGB images. Flattens image sequences."""
135
136 - def __init__(self, name='TkDisplay', **kw):
137 kw['name'] = name
138 apply(NetworkNode.__init__, (self,), kw)
139
140 import Tkinter, ImageTk
141
142 self.top = Tkinter.Toplevel()
143 self.imCanvas = Tkinter.Canvas(self.top)
144 self.canvasImage = None
145
146 code = """def doit(self, img):
147 import Image, Tkinter, ImageTk, Numeric
148
149 if len(img) > 1:
150 img = toSeq(img)
151 img = flatten(img)
152 else:
153 img = img[0]
154
155 Aim = img.toarray()
156 Aim = Aim[:,:,0:3]
157 Aim = Aim / 257
158 h, w, c = Aim.shape
159 Aim = Numeric.reshape(Aim, (1, w*h*c))
160 Aim = Aim.astype(Numeric.UnsignedInt8)
161
162 PILimage = Image.fromstring('RGB', (w,h), Aim.tostring())
163
164 # This code from PILNodes.py written by Michel Sanner
165 if PILimage:
166 if self.canvasImage: self.imCanvas.delete(self.canvasImage)
167 self.imtk = ImageTk.PhotoImage(PILimage)
168 self.canvasImage = self.imCanvas.create_image(
169 self.imtk.width()+2, self.imtk.height()+2, image=self.imtk, anchor='se')
170 self.imCanvas.configure(width=self.imtk.width()+2,
171 height=self.imtk.height()+2)
172 self.imCanvas.pack()\n"""
173
174 self.setFunction(code)
175
176 self.inputPortsDescr.append({
177 'name': 'img',
178 'singleConnection':False,
179 'datatype': 'imimage'})
180
182 """Uses ImageMagick to display an image"""
183
184 - def __init__(self, name='Display', **kw):
185 kw['name'] = name
186 apply(NetworkNode.__init__, (self,), kw)
187
188 code = """def doit(self, img):
189 img = toSeq(img)
190 img.display()\n"""
191
192 self.setFunction(code)
193
194 self.inputPortsDescr.append({
195 'name': 'img',
196 'singleConnection':False,
197 'datatype': 'imimage'})
198
200 """Animates an image sequence on the screen."""
201
202 - def __init__(self, name='Animate', **kw):
203 kw['name'] = name
204 apply(NetworkNode.__init__, (self,), kw)
205
206 code = """def doit(self, img):
207 img = toSeq(img)
208 img.animate()\n"""
209
210 self.setFunction(code)
211
212 self.inputPortsDescr.append({
213 'name': 'img',
214 'singleConnection':False,
215 'datatype': 'imimage'})
216
217
218
219 -class Copy(NetworkNode):
220 """Copies an image.
221
222 Usage Example: Add the an image to a sequence twice"""
223
225 kw['name'] = name
226 apply(NetworkNode.__init__, (self,), kw)
227
228 code = """def doit(self, img):
229 img = toSeq(img)
230 result = img.copy()
231 self.outputData(outImage=result)\n"""
232
233 self.setFunction(code)
234
235 self.inputPortsDescr.append({
236 'name': 'img',
237 'singleConnection':False,
238 'datatype': 'imimage'})
239 self.outputPortsDescr.append({
240 'name': 'outImage',
241 'datatype': 'imimage'})
242
244 """Converts an image to a Numeric array."""
245
246 - def __init__(self, name='toArrray', **kw):
247 kw['name'] = name
248 apply(NetworkNode.__init__, (self,), kw)
249
250 code = """def doit(self, img):
251 img = toSeq(img)
252 result = img.toarray()
253 self.outputData(out=result)\n"""
254
255 self.setFunction(code)
256
257 self.inputPortsDescr.append({
258 'name': 'img',
259 'singleConnection':False,
260 'datatype': 'imimage'})
261 self.outputPortsDescr.append({
262 'name': 'out'})
263
265 """Outpus an array of pixel data for the region specified.
266
267 X: X coordinate of top left corner of region
268 Y: Y coordinate of top left corner of region
269 Rows: height of region
270 Cols: length of region"""
271
272 - def __init__(self, name='getPixels', **kw):
273 kw['name'] = name
274 apply(NetworkNode.__init__, (self,), kw)
275
276 code = """def doit(self, img, x, y, c, r):
277 img = toSeq(img)
278 x = int(x)
279 y = int(y)
280 r = int(r)
281 c = int(c)
282 result = img.getpixels(x,y,c,r)
283 self.outputData(out=result)\n"""
284
285 self.setFunction(code)
286
287 self.widgetDescr['x'] = {
288 'class': 'NEEntry',
289 'master': 'node',
290 'initialValue': 'X',
291 'width': 10,}
292 self.widgetDescr['y'] = {
293 'class': 'NEEntry',
294 'master': 'node',
295 'initialValue': 'Y',
296 'width': 10,}
297 self.widgetDescr['c'] = {
298 'class': 'NEEntry',
299 'master': 'node',
300 'initialValue': 'Columns',
301 'width': 10,}
302 self.widgetDescr['r'] = {
303 'class': 'NEEntry',
304 'master': 'node',
305 'initialValue': 'Rows',
306 'width': 10,}
307
308 self.inputPortsDescr.append({
309 'name': 'img',
310 'singleConnection':False,
311 'datatype': 'imimage'})
312 self.inputPortsDescr.append({
313 'name': 'x',
314 'datatype': 'str'})
315 self.inputPortsDescr.append({
316 'name': 'y',
317 'datatype': 'str'})
318 self.inputPortsDescr.append({
319 'name': 'c',
320 'datatype': 'str'})
321 self.inputPortsDescr.append({
322 'name': 'r',
323 'datatype': 'str'})
324 self.outputPortsDescr.append({
325 'name': 'out'})
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367 -class Halve(NetworkNode):
368 """Minifies an image to half its size."""
369
370 - def __init__(self, name='Halve', **kw):
371 kw['name'] = name
372 apply(NetworkNode.__init__, (self,), kw)
373
374 code = """def doit(self, img):
375 img = toSeq(img)
376 result = minify(img)
377 self.outputData(outImage=result)\n"""
378
379 self.setFunction(code)
380
381 self.inputPortsDescr.append({
382 'name': 'img',
383 'singleConnection':False,
384 'datatype': 'imimage'})
385 self.outputPortsDescr.append({
386 'name': 'outImage',
387 'datatype': 'imimage'})
388
390 """Magnifies an image to twice its size."""
391
392 - def __init__(self, name='Double', **kw):
393 kw['name'] = name
394 apply(NetworkNode.__init__, (self,), kw)
395
396 code = """def doit(self, img):
397 img = toSeq(img)
398 result = magnify(img)
399 self.outputData(outImage=result)\n"""
400
401 self.setFunction(code)
402
403 self.inputPortsDescr.append({
404 'name': 'img',
405 'singleConnection':False,
406 'datatype': 'imimage'})
407 self.outputPortsDescr.append({
408 'name': 'outImage',
409 'datatype': 'imimage'})
410
412 """Resizes an image by a factor. Maintains aspect ratio.
413
414 Factor: the factor by which to change the size (2.0 would double the size; 0.5 would halve it)"""
415
416 - def __init__(self, name='ResizeFactor', **kw):
417 kw['name'] = name
418 apply(NetworkNode.__init__, (self,), kw)
419
420 code = """def doit(self, img, fact):
421 img = toSeq(img)
422 result = img.copy()
423 if scale:
424 op = eval('(-1,' + str(fact) + ')')
425 result = imresize(img, op)
426 self.outputData(outImage=result)\n"""
427
428 self.setFunction(code)
429
430 self.widgetDescr['fact'] = {
431 'class': 'NEDial',
432 'labelCfg':{'text':'Factor'}, 'labelSide':'left',
433 'type': float,
434 'initialValue': 1.0,
435 'min': 0.0,
436 'max': None,
437 'oneTurn': 1.0,
438 'master': 'node',
439 'size': 75,}
440
441 self.inputPortsDescr.append({
442 'name': 'img',
443 'singleConnection':False,
444 'datatype': 'imimage'})
445 self.inputPortsDescr.append({
446 'name': 'fact',
447 'datatype': 'float'})
448 self.outputPortsDescr.append({
449 'name': 'outImage',
450 'datatype': 'imimage'})
451
453 """Resizes an image to the given width. Maintains aspect ratio.
454
455 Width: the new width"""
456
457 - def __init__(self, name='ResizeWidth', **kw):
458 kw['name'] = name
459 apply(NetworkNode.__init__, (self,), kw)
460
461 code = """def doit(self, img, width):
462 img = toSeq(img)
463 result = img.copy()
464 if width:
465 op = eval('(-1,' + width + ')')
466 result = imresize(img, op)
467 self.outputData(outImage=result)\n"""
468
469 self.setFunction(code)
470
471 self.widgetDescr['width'] = {
472 'class': 'NEEntry',
473 'master': 'node',
474 'width': 10,}
475
476 self.inputPortsDescr.append({
477 'name': 'img',
478 'singleConnection':False,
479 'datatype': 'imimage'})
480 self.inputPortsDescr.append({
481 'name': 'width',
482 'datatype': 'string'})
483 self.outputPortsDescr.append({
484 'name': 'outImage',
485 'datatype': 'imimage'})
486
488 """Resizes an image to a given height. Maintains aspect ratio.
489
490 Height: the new height"""
491
492 - def __init__(self, name='ResizeHeight', **kw):
493 kw['name'] = name
494 apply(NetworkNode.__init__, (self,), kw)
495
496 code = """def doit(self, img, height):
497 img = toSeq(img)
498 result = img.copy()
499 if height:
500 op = eval('(' + height + ',-1)')
501 result = imresize(img, op)
502 self.outputData(outImage=result)\n"""
503
504 self.setFunction(code)
505
506 self.widgetDescr['height'] = {
507 'class': 'NEEntry',
508 'master': 'node',
509 'width': 10,}
510
511 self.inputPortsDescr.append({
512 'name': 'img',
513 'singleConnection':False,
514 'datatype': 'imimage'})
515 self.inputPortsDescr.append({
516 'name': 'height',
517 'datatype': 'string'})
518 self.outputPortsDescr.append({
519 'name': 'outImage',
520 'datatype': 'imimage'})
521
523 """Resizes an image to a given width and height. Does not (necessarily) maintain aspect ratio.
524
525 Width: the new width
526 Height: the new height"""
527
528 - def __init__(self, name='ResizeDim', **kw):
529 kw['name'] = name
530 apply(NetworkNode.__init__, (self,), kw)
531
532 code = """def doit(self, img, width, height):
533 img = toSeq(img)
534 result = img.copy()
535 if width and height:
536 op = eval('(' + height + ',' + width + ')')
537 result = imresize(img, op)
538 self.outputData(outImage=result)\n"""
539
540 self.setFunction(code)
541
542 self.widgetDescr['width'] = {
543 'class': 'NEEntry',
544 'master': 'node',
545 'initialValue': 'Width',
546 'width': 5}
547 self.widgetDescr['height'] = {
548 'class': 'NEEntry',
549 'master': 'node',
550 'initialValue': 'Height',
551 'width': 5}
552
553 self.inputPortsDescr.append({
554 'name': 'img',
555 'singleConnection':False,
556 'datatype': 'imimage'})
557 self.inputPortsDescr.append({
558 'name': 'width',
559 'datatype': 'string'})
560 self.inputPortsDescr.append({
561 'name': 'height',
562 'datatype': 'string'})
563 self.outputPortsDescr.append({
564 'name': 'outImage',
565 'datatype': 'imimage'})
566
568 """Resamples an image to a given width. Maintains aspect ratio.
569
570 Width: the new width"""
571
572 - def __init__(self, name='SampleWidth', **kw):
573 kw['name'] = name
574 apply(NetworkNode.__init__, (self,), kw)
575
576 code = """def doit(self, img, width):
577 img = toSeq(img)
578 result = img.copy()
579 if width:
580 op = eval('(-1,' + width + ')')
581 result = sample(img, op)
582 self.outputData(outImage=result)\n"""
583
584 self.setFunction(code)
585
586 self.widgetDescr['width'] = {
587 'class': 'NEEntry',
588 'master': 'node',
589 'width': 10,}
590
591 self.inputPortsDescr.append({
592 'name': 'img',
593 'singleConnection':False,
594 'datatype': 'imimage'})
595 self.inputPortsDescr.append({
596 'name': 'width',
597 'datatype': 'string'})
598 self.outputPortsDescr.append({
599 'name': 'outImage',
600 'datatype': 'imimage'})
601
603 """Resamples an image to a given height. Maintains aspect ratio.
604
605 Height: the new height"""
606
607 - def __init__(self, name='SampleHeight', **kw):
608 kw['name'] = name
609 apply(NetworkNode.__init__, (self,), kw)
610
611 code = """def doit(self, img, height):
612 img = toSeq(img)
613 result = img.copy()
614 if height:
615 op = eval('(' + height + ',-1)')
616 result = sample(img, op)
617 self.outputData(outImage=result)\n"""
618
619 self.setFunction(code)
620
621 self.widgetDescr['height'] = {
622 'class': 'NEEntry',
623 'master': 'node',
624 'width': 10,}
625
626 self.inputPortsDescr.append({
627 'name': 'img',
628 'singleConnection':False,
629 'datatype': 'imimage'})
630 self.inputPortsDescr.append({
631 'name': 'height',
632 'datatype': 'string'})
633 self.outputPortsDescr.append({
634 'name': 'outImage',
635 'datatype': 'imimage'})
636
638 """Resamples an image to a given width and height. Does not (necessarily) maintain aspect ratio.
639
640 Width: the new width
641 Height: the new height"""
642
643 - def __init__(self, name='SampleDim', **kw):
644 kw['name'] = name
645 apply(NetworkNode.__init__, (self,), kw)
646
647 code = """def doit(self, img, width, height):
648 img = toSeq(img)
649 result = img.copy()
650 if width and height:
651 op = eval('(' + height + ',' + width + ')')
652 result = sample(img, op)
653 self.outputData(outImage=result)\n"""
654
655 self.setFunction(code)
656
657 self.widgetDescr['width'] = {
658 'class': 'NEEntry',
659 'master': 'node',
660 'initialValue': 'Width',
661 'width': 5}
662 self.widgetDescr['height'] = {
663 'class': 'NEEntry',
664 'master': 'node',
665 'initialValue': 'Height',
666 'width': 5}
667
668 self.inputPortsDescr.append({
669 'name': 'img',
670 'singleConnection':False,
671 'datatype': 'imimage'})
672 self.inputPortsDescr.append({
673 'name': 'width',
674 'datatype': 'string'})
675 self.inputPortsDescr.append({
676 'name': 'height',
677 'datatype': 'string'})
678 self.outputPortsDescr.append({
679 'name': 'outImage',
680 'datatype': 'imimage'})
681
683 """Rescales an image to a given width. Maintains aspect ratio.
684
685 Width: the new width"""
686
687 - def __init__(self, name='ScaleWidth', **kw):
688 kw['name'] = name
689 apply(NetworkNode.__init__, (self,), kw)
690
691 code = """def doit(self, img, width):
692 img = toSeq(img)
693 result = img.copy()
694 if width:
695 op = eval('(-1,' + width + ')')
696 result = scale(img, op)
697 self.outputData(outImage=result)\n"""
698
699 self.setFunction(code)
700
701 self.widgetDescr['width'] = {
702 'class': 'NEEntry',
703 'master': 'node',
704 'width': 10,}
705
706 self.inputPortsDescr.append({
707 'name': 'img',
708 'singleConnection':False,
709 'datatype': 'imimage'})
710 self.inputPortsDescr.append({
711 'name': 'width',
712 'datatype': 'string'})
713 self.outputPortsDescr.append({
714 'name': 'outImage',
715 'datatype': 'imimage'})
716
718 """Rescales an image to a given height. Maintains aspect ratio.
719
720 Height: the new height"""
721
722 - def __init__(self, name='ScaleHeight', **kw):
723 kw['name'] = name
724 apply(NetworkNode.__init__, (self,), kw)
725
726 code = """def doit(self, img, height):
727 img = toSeq(img)
728 result = img.copy()
729 if height:
730 op = eval('(' + height + ',-1)')
731 result = scale(img, op)
732 self.outputData(outImage=result)\n"""
733
734 self.setFunction(code)
735
736 self.widgetDescr['height'] = {
737 'class': 'NEEntry',
738 'master': 'node',
739 'width': 10,}
740
741 self.inputPortsDescr.append({
742 'name': 'img',
743 'singleConnection':False,
744 'datatype': 'imimage'})
745 self.inputPortsDescr.append({
746 'name': 'height',
747 'datatype': 'string'})
748 self.outputPortsDescr.append({
749 'name': 'outImage',
750 'datatype': 'imimage'})
751
753 """Rescales an image to a given width and height. Does not (necessarily) maintain aspect ratio.
754
755 Width: the new width
756 Height: the new height"""
757
758 - def __init__(self, name='ScaleDim', **kw):
759 kw['name'] = name
760 apply(NetworkNode.__init__, (self,), kw)
761
762 code = """def doit(self, img, width, height):
763 img = toSeq(img)
764 result = img.copy()
765 if width and height:
766 op = eval('(' + height + ',' + width + ')')
767 result = scale(img, op)
768 self.outputData(outImage=result)\n"""
769
770 self.setFunction(code)
771
772 self.widgetDescr['width'] = {
773 'class': 'NEEntry',
774 'master': 'node',
775 'initialValue': 'Width',
776 'width': 5}
777 self.widgetDescr['height'] = {
778 'class': 'NEEntry',
779 'master': 'node',
780 'initialValue': 'Height',
781 'width': 5}
782
783 self.inputPortsDescr.append({
784 'name': 'img',
785 'singleConnection':False,
786 'datatype': 'imimage'})
787 self.inputPortsDescr.append({
788 'name': 'width',
789 'datatype': 'string'})
790 self.inputPortsDescr.append({
791 'name': 'height',
792 'datatype': 'string'})
793 self.outputPortsDescr.append({
794 'name': 'outImage',
795 'datatype': 'imimage'})
796
798 """Resizes an image by a factor for a thumbnail. Maintains aspect ratio.
799
800 Factor: the factor by which to change the size (2.0 would double the size; 0.5 would halve it)"""
801
802 - def __init__(self, name='ThumbFactor', **kw):
803 kw['name'] = name
804 apply(NetworkNode.__init__, (self,), kw)
805
806 code = """def doit(self, img, fact):
807 img = toSeq(img)
808 result = img.copy()
809 if scale:
810 op = eval('(-1,' + str(fact) + ')')
811 result = thumbnail(img, op)
812 self.outputData(outImage=result)\n"""
813
814 self.setFunction(code)
815
816 self.widgetDescr['fact'] = {
817 'class': 'NEDial',
818 'labelCfg':{'text':'Factor'}, 'labelSide':'left',
819 'type': float,
820 'initialValue': 1.0,
821 'min': 0.0,
822 'max': None,
823 'oneTurn': 1.0,
824 'master': 'node',
825 'size': 75,}
826
827 self.inputPortsDescr.append({
828 'name': 'img',
829 'singleConnection':False,
830 'datatype': 'imimage'})
831 self.inputPortsDescr.append({
832 'name': 'fact',
833 'datatype': 'float'})
834 self.outputPortsDescr.append({
835 'name': 'outImage',
836 'datatype': 'imimage'})
837
839 """Resizes an image to the given width for a thumbnail. Maintains aspect ratio.
840
841 Width: the new width"""
842
843 - def __init__(self, name='ThumbWidth', **kw):
844 kw['name'] = name
845 apply(NetworkNode.__init__, (self,), kw)
846
847 code = """def doit(self, img, width):
848 img = toSeq(img)
849 result = img.copy()
850 if width:
851 op = eva