Changeset 288
- Timestamp:
- 12/30/07 20:56:37 (12 months ago)
- Location:
- libraries/coreimage
- Files:
-
- 3 modified
-
__init__.py (modified) (15 diffs)
-
coreimage-example5.py (modified) (4 diffs)
-
coreimage-example6.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libraries/coreimage/__init__.py
r235 r288 5 5 6 6 __author__ = "Tom De Smedt" 7 __version__ = "1.9. 1"7 __version__ = "1.9.2" 8 8 __copyright__ = "Copyright (c) 2007 Tom De Smedt" 9 9 __license__ = "MIT" … … 37 37 LAYER_LAYERS = "layers" 38 38 LAYER_PATH = "path" 39 LAYER_PIXELS = "pixels" 39 40 LAYER_CIIMAGEOBJECT = "CIImageobject" 40 41 LAYER_NSIMAGEDATA = "NSImageData" … … 321 322 except: pass 322 323 324 # Pixels 325 # A one-dimensional list of color objects. 326 try: 327 clr, w, h = data[0][0], data[1], data[2] 328 r, g, b, a = clr.r, clr.g, clr.b, clr.a 329 type = LAYER_PIXELS 330 return _add_layer(data, type, x, y, w, h) 331 except: pass 332 323 333 # Core Image CIImage object. 324 334 try: … … 420 430 421 431 return self.layer((path, background, foreground, stroke, strokewidth), LAYER_PATH, x, y, w, h, name) 432 433 def layer_pixels(self, colors, w, h, 434 x=None, y=None, name=""): 435 436 """ Adds a new layer from a list of pixel colors. 437 """ 438 439 return self.layer((colors, w, h), LAYER_PIXELS, x, y, w, h, name) 422 440 423 441 def layer_bytes(self, data, … … 427 445 428 446 Creates an NSImage from the data. 429 This method is should go somewhere because it references Cocoa.447 This method should go somewhere else because it references Cocoa. 430 448 431 449 """ … … 580 598 def is_file(self) : return (self.type == LAYER_FILE) 581 599 def is_fill(self) : return (self.type == LAYER_FILL) 582 def is_gradient(self) : return (self.type == LAYER_LINEAR_GRADIENT) 600 def is_gradient(self) : return (self.type == LAYER_LINEAR_GRADIENT or 601 self.type == LAYER_RADIAL_GRADIENT) 583 602 def is_linear_gradient(self) : return (self.type == LAYER_RADIAL_GRADIENT) 584 603 def is_radial_gradient(self) : return (self.type == LAYER_LINEAR_GRADIENT) 585 604 def is_path(self) : return (self.type == LAYER_PATH) 605 def is_pixels(self) : return (self.type == LAYER_PIXELS) 606 def is_group(self) : return (self.type == LAYER_LAYERS) 586 607 def has_layers(self) : return (self.type == LAYER_LAYERS) 587 608 def is_mask(self) : return (self.canvas._parent != None and … … 661 682 if self.type == LAYER_PATH: 662 683 data = (self.data[0].copy(), Color(self.data[1]), Color(self.data[2]), Color(self.data[3]), self.data[4]) 684 if self.type == LAYER_PIXELS: 685 data = self.data 663 686 if self.type == LAYER_CIIMAGEOBJECT: 664 687 data = self.data … … 1418 1441 path.stroke() 1419 1442 img.unlockFocus() 1443 img = img.TIFFRepresentation() 1444 img = CIImage.alloc().initWithData_(img) 1445 1446 if type == LAYER_PIXELS: 1447 1448 # The layer's source is a list of pixel colors. 1449 # Duane Bailey, http://nodebox.net/code/index.php/shared_2007-12-28-23-34-57 1450 # If there is more width*height than there are pixels, 1451 # the bitmap will be completed with transparent pixels. 1452 1453 p, w, h = data 1454 import Numeric 1455 raw = Numeric.array([0] * w*h*4, typecode='c') 1456 for i in range(len(p)): 1457 raw[i*4+0] = p[i].r * 255 1458 raw[i*4+1] = p[i].g * 255 1459 raw[i*4+2] = p[i].b * 255 1460 raw[i*4+3] = p[i].a * 255 1461 raw = (raw, None, None, None, None) 1462 img = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_( 1463 raw, w, h, 8, 4, True, False, NSDeviceRGBColorSpace, w * 4, 32 1464 ) 1420 1465 img = img.TIFFRepresentation() 1421 1466 img = CIImage.alloc().initWithData_(img) … … 2416 2461 self._img = self._img.TIFFRepresentation() 2417 2462 self._img = NSBitmapImageRep.imageRepWithData_(self._img) 2418 2419 raw = self._img.bitmapData() 2420 a = Numeric.fromstring(raw, Numeric.UnsignedInt8) 2421 2463 2422 2464 self.channels = self._img.samplesPerPixel() 2423 self.pixels = Numeric.reshape(a, (self.w*self.h, self.channels)) 2424 2425 if self.is_rgb(): 2426 self.r = self.pixels[:,0] 2427 self.g = self.pixels[:,1] 2428 self.b = self.pixels[:,2] 2429 self.a = None 2430 if self.has_alpha(): self.a = self. pixels[:,3] 2431 2465 2432 2466 def is_rgb(self): 2433 2467 … … 2440 2474 def get_pixel(self, x, y): 2441 2475 2442 """ Returns color values for the pixel at x, y. 2443 """ 2444 2445 if 0 <= x < self.w and 0 <= y < self.h: 2446 return list(self.pixels[x+y*self.w]) 2447 else: 2448 return None 2449 2450 def min(self, transparency=False, treshold=0): 2476 """ Returns color for the pixel at x, y. 2477 """ 2478 2479 return _ctx.color(self._img.colorAtX_y_(x, y)) 2480 2481 def set_pixel(self, x, y, clr): 2482 2483 """ Sets the pixel at x, y with the given color. 2484 """ 2485 2486 self._img.setColor_atX_y_(clr._rgb, x, y) 2487 2488 def update(self): 2489 2490 """ Updates the layer with the changes from set_pixel(). 2491 """ 2492 2493 self.layer.data = self._img.TIFFRepresentation() 2494 self.layer.type = LAYER_NSIMAGEDATA 2495 2496 def _array(self, int=8): 2497 2498 """ All the pixel color values as a lists of integers. 2499 """ 2500 2501 bytes = self._img.bitmapData() 2502 a = Numeric.fromstring(bytes, Numeric.UnsignedInt8) 2503 a = Numeric.reshape(a, (self.w*self.h, self.channels)) 2504 if int == 32: 2505 a = a.astype(Numeric.UnsignedInt32) 2506 2507 return a 2508 2509 def min(self, transparency=False, threshold=0): 2451 2510 2452 2511 """ Returns the lowest color values in the image. 2453 2512 2454 2513 By default, transparent pixels (alpha=0) are 2455 not searched. The treshold defines the cutoff, 2456 for example treshold=100 means that all pixels with alpha 2457 less than 100 are not taken into account. 2458 2459 """ 2460 2461 a = self.pixels.astype(Numeric.UnsignedInt32) 2514 not searched. The threshold defines the cutoff, 2515 for example threshold=0.1 means that all pixels with alpha 2516 less than 10% are not taken into account. 2517 2518 """ 2519 2520 if isinstance(threshold, int): 2521 threshold /= 255.0 2522 2523 a = self._array(int=32) 2462 2524 if transparency == False and self.has_alpha(): 2463 i = Numeric.nonzero(Numeric.greater(self.a, t reshold))2525 i = Numeric.nonzero(Numeric.greater(self.a, threshold)) 2464 2526 a = Numeric.take(a, i) 2465 2527 … … 2468 2530 for j in range(self.channels): 2469 2531 v.append(a[i[j]][j]) 2470 return v 2471 2472 def max(self, transparency=False, treshold=0): 2532 2533 # Return a color object: 2534 clr = _ctx.color(0) 2535 clr.r, clr.g, clr.b = v[0]/255.0, v[1]/255.0, v[2]/255.0 2536 if self.has_alpha(): 2537 clr.a = v[3]/255.0 2538 return clr 2539 2540 def max(self, transparency=False, threshold=0): 2473 2541 2474 2542 """ Returns the highest color values in the image. 2475 2543 """ 2476 2544 2477 a = self.pixels.astype(Numeric.UnsignedInt32) 2545 if isinstance(threshold, int): 2546 threshold /= 255.0 2547 2548 a = self._array(int=32) 2478 2549 if transparency == False and self.has_alpha(): 2479 i = Numeric.nonzero(Numeric.greater(self.a, t reshold))2550 i = Numeric.nonzero(Numeric.greater(self.a, threshold)) 2480 2551 a = Numeric.take(a, i) 2481 2552 … … 2484 2555 for j in range(self.channels): 2485 2556 v.append(a[i[j]][j]) 2486 return v 2487 2488 def average(self, transparency=False, treshold=0): 2557 2558 # Return a color object: 2559 clr = _ctx.color(0) 2560 clr.r, clr.g, clr.b = v[0]/255.0, v[1]/255.0, v[2]/255.0 2561 if self.has_alpha(): 2562 clr.a = v[3]/255.0 2563 return clr 2564 2565 def average(self, transparency=False, threshold=0): 2489 2566 2490 2567 """ Returns the average color values in the image. 2491 2568 """ 2492 2569 2493 a = self.pixels.astype(Numeric.UnsignedInt32) 2494 n = len(self.pixels) 2495 2570 a = self._array(int=32) 2571 n = len(a) 2572 2573 if isinstance(threshold, int): 2574 threshold /= 255.0 2496 2575 if transparency == False and self.has_alpha(): 2497 i = Numeric.nonzero(Numeric.greater(self.a, t reshold))2576 i = Numeric.nonzero(Numeric.greater(self.a, threshold)) 2498 2577 a = Numeric.take(a, i) 2499 2578 n = len(i) … … 2504 2583 for i in range(self.channels): 2505 2584 v.append(int(a[i])) 2506 return v 2585 2586 # Return a color object: 2587 clr = _ctx.color(0) 2588 clr.r, clr.g, clr.b = v[0]/255.0, v[1]/255.0, v[2]/255.0 2589 if self.has_alpha(): 2590 clr.a = v[3]/255.0 2591 return clr 2507 2592 2508 2593 def histogram(self, channel=None, helper=False): … … 2517 2602 """ 2518 2603 2604 a = self._array() 2605 2519 2606 bins = Numeric.arange(1, 255, 1) 2520 2607 v = [] 2521 2608 for i in range(self.channels): 2522 p = self.pixels[:,i]2609 p = a[:,i] 2523 2610 2524 2611 # From the NumPy searchsorted() documentation. … … 2903 2990 c.draw() 2904 2991 2992 # 1.9.2 2993 # Added layers_pixels() to Canvas. 2994 # Added set_pixel() to CoreImagePixels. 2995 # All of the CoreImagePixels methods, except histogram(), 2996 # now use color() objects instead of lists of R,G,B values. 2997 2905 2998 # 1.9.0 2906 2999 # Layers from bytes (e.g. download stream) can be created with Canvas.layer_bytes(). -
libraries/coreimage/coreimage-example5.py
r181 r288 1 size(500, 500) 1 # Coffee dynamics. 2 2 3 3 try: … … 7 7 reload(coreimage) 8 8 9 size(500, 500) 9 10 speed(30) 10 11 def setup(): … … 24 25 25 26 # The bottom layer is a brownish fill color. 26 l = canvas.layer_fill(color(0. 2,0.15,0))27 l = canvas.layer_fill(color(0.07,0.05,0)) 27 28 28 29 if not copy: … … 30 31 # construct a layer from an outlined text path. 31 32 fontsize(100) 32 p = textpath(" SMOKE", 0, 50)33 copy = canvas.layer_path(p, foreground=color(1 ))33 p = textpath("COFFEE", 0, 50) 34 copy = canvas.layer_path(p, foreground=color(1,1,0.95)) 34 35 else: 35 36 # During the next frames of the animation, -
libraries/coreimage/coreimage-example6.py
r234 r288 50 50 c.draw() 51 51 52 """ 52 53 53 # Some fun converting pixels to curves. 54 54 # Uncomment the code below to grow grass on a sphere. 55 55 """ 56 56 l.adjust_contrast(1.1) 57 57 58 58 p = l.pixels() 59 59 60 colormode(RGB, 255)61 60 nofill() 62 61 strokewidth(0.25) … … 73 72 # more requires to much processing and clogs the image. 74 73 if random() > 0.5: 75 r,g,b,a= p.get_pixel(i,j)74 clr = p.get_pixel(i,j) 76 75 77 76 # We don't draw curves on black pixels. 78 if not ( r == g ==b == 0):77 if not (clr.r == clr.g == clr.b == 0): 79 78 80 79 # The actual location of the curve … … 88 87 dy = y-l.y 89 88 dh = 20 90 91 c = color(r, g, b) 92 c.b += random(1.5) 93 stroke(c) 94 89 90 stroke(clr) 95 91 beginpath(x, y) 96 92 curveto(x+random(dh),
