Package Vision :: Module ImageMagickNodes
[hide private]
[frames] | no frames]

Source Code for Module Vision.ImageMagickNodes

   1  ############################################################
 
   2  #       ImageMagick for Vision
 
   3  #
 
   4  #   Vision: http://www.scripps.edu/~sanner/python/vision/
 
   5  #   ImageMagick: http://www.imagemagick.org
 
   6  #   PythonMagick: http://sourceforge.net/projects/pylab
 
   7  #
 
   8  #   Author: Natalie Rubin (nmrubin@sdsc.edu)
 
   9  #   Based on the PIL nodes included with Vision
 
  10  ############################################################
 
  11  
 
  12  from NetworkEditor.items import NetworkNode 
  13  from magick import * 
  14  
 
  15  ### Convert list of imimages to a sequence ###
 
16 -def toSeq(imgList):
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 ### Input/Output ###
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
31 - def __init__(self, name='Read', **kw):
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
59 -class Write(NetworkNode):
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
64 - def __init__(self, name='Write', **kw):
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
92 -class toPIL(NetworkNode):
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
97 - def __init__(self, name='toPIL', **kw):
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
131 -class TkDisplay(NetworkNode):
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 # This code from PILNodes.py written by Michel Sanner 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
181 -class Display(NetworkNode):
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
199 -class Animate(NetworkNode):
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
224 - def __init__(self, name='Copy', **kw):
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
243 -class toArray(NetworkNode):
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
264 -class getPixels(NetworkNode):
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 ### Memory error 328 ##class Describe(NetworkNode): 329 ## """Outputs attributes to a file.""" 330 ## 331 ## def __init__(self, name='Describe', **kw): 332 ## kw['name'] = name 333 ## apply(NetworkNode.__init__, (self,), kw) 334 ## 335 ## code = """def doit(self, img, filename, verb): 336 ## if filename and filename != 'File Name': 337 ## img.describe(verb, filename)\n""" 338 ## 339 ## self.setFunction(code) 340 ## 341 ## fileTypes = [('all', '*')] 342 ## self.widgetDescr['filename'] = { 343 ## 'class': 'NEEntryWithFileSaver', 344 ## 'master': 'node', 345 ## 'initialValue': 'File Name', 346 ## 'filetypes': fileTypes, 347 ## 'title': 'Write Image', 348 ## 'width': 10 } 349 ## self.widgetDescr['verb'] = { 350 ## 'class': 'NECheckButton', 351 ## 'initialValue': 0, 352 ## 'text': 'Verbose', 353 ## 'master': 'node',} 354 ## 355 ## self.inputPortsDescr.append({ 356 ## 'name': 'img', 357 ## 'singleConnection':False, 358 ## 'datatype': 'imimage'}) 359 ## self.inputPortsDescr.append({ 360 ## 'name': 'filename', 361 ## 'datatype': 'str'}) 362 ## self.inputPortsDescr.append({ 363 ## 'name': 'verb', 364 ## 'datatype': 'int'}) 365 366 ### Size ###
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
389 -class Double(NetworkNode):
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
411 -class ResizeFactor(NetworkNode):
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.