The Flying Meat Wiki : AcornPythonNewFadedImageWithRoundCorners

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register
Oldest known version of this page was edited on 2008-04-09 20:41:42 by GusMueller []
Page view:
Here's an example python plugin that will make a new image, fade it a bit, and add around borders. Just place it in a text file named "Faded.py" and place it in your ~/Library/Application Support/Acorn/Plug-Ins/ folder, and restart Acorn.

Before and after:
image image

Faded.py
# Just copy this file to:
# ~/Library/Application Support/Acorn/Plug-Ins/.
# and away you go!

import objc
from Foundation import *
from AppKit import *

ACScriptSuperMenuTitle = "Old Skool"
ACScriptMenuTitle = "Faded with corners"
ACShortcutKey = "o"
ACShortcutMask = "control command"

CIImage  = objc.lookUpClass('CIImage')
CIVector = objc.lookUpClass('CIVector')
CIColor  = objc.lookUpClass('CIColor')
CIFilter = objc.lookUpClass('CIFilter')

def makePolaroid(image):
   
    color = CIColor.colorWithRed_green_blue_alpha_(1, 1, 0, 0.1)

    colorFilter = CIFilter.filterWithName_('CIConstantColorGenerator')
    colorFilter.setDefaults()
    colorFilter.setValue_forKey_(color, 'inputColor')
   
    filter = CIFilter.filterWithName_('CILightenBlendMode')
    filter.setDefaults()
   
    filter.setValue_forKey_(image, 'inputBackgroundImage')
    filter.setValue_forKey_(colorFilter.valueForKey_('outputImage'), 'inputImage')
   
    return filter.valueForKey_('outputImage')
   
   

def cicrop(image, originalExtent):
    # the CIColor makes the extent of the image kinda large, so we need to bring it back to earth
    inputRectangle = CIVector.vectorWithX_Y_Z_W_(0, 0, originalExtent[1][0], originalExtent[1][1])
   
    cropFilter = CIFilter.filterWithName_('CICrop')
    cropFilter.setDefaults()
    cropFilter.setValue_forKey_(image, 'inputImage')
    cropFilter.setValue_forKey_(inputRectangle, 'inputRectangle')
   
    return cropFilter.valueForKey_('outputImage')
   

def addCorners(image):
   
    cornerRadius    = 20
   
    nsimg           = image.NSImage()
    size            = nsimg.size()
   
    rect            = ((0,0), size)
    pb              = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_(rect, cornerRadius, cornerRadius)
   
    newImage        = NSImage.alloc().initWithSize_(size)
   
    newImage.lockFocus()
   
    pb.setClip()
   
    nsimg.drawAtPoint_fromRect_operation_fraction_((0, 0), NSZeroRect, NSCompositeCopy, 1.0)
   
    newImage.unlockFocus()
   
    return newImage

def main(image):
   
    originalExtent = image.extent()
   
    image       = makePolaroid(image)
    image       = cicrop(image, originalExtent)
    nsImage     = addCorners(image)
   
    NSDocumentController.sharedDocumentController().newDocumentWithImageData_(nsImage.TIFFRepresentation())
   
    return None




Back to AcornPlugins
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.3
Page was generated in 0.0914 seconds