The Flying Meat Wiki : AcornPythonNewGrayscaleImageWithRoundCorners

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register
Here's an example python plugin that will make a new image, turn it grayscale, give it a slight blur, and add around borders. Just place it in a text file named "NewImageWithRoundCorners.py" and place it in your ~/Library/Application Support/Acorn/Plug-Ins/ folder, and restart Acorn.

Before and after:
image image

NewImageWithRoundCorners.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 = "New Image With Round Corners"
ACShortcutKey = "o"
ACShortcutMask = "control command"

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


def makeGrayscale(image):
   
    color = CIColor.colorWithRed_green_blue_(0.5, 0.5, 0.5)
    filter = CIFilter.filterWithName_('CIColorMonochrome')
    filter.setDefaults()
    filter.setValue_forKey_(image, 'inputImage')
    filter.setValue_forKey_(color, 'inputColor')
    filter.setValue_forKey_(1, 'inputIntensity')
   
    return filter.valueForKey_('outputImage')
   
def blurImage(image):
   
    originalExtent = image.extent()
   
    filter = CIFilter.filterWithName_('CIGaussianBlur')
    filter.setDefaults()
    filter.setValue_forKey_(image, 'inputImage')
    filter.setValue_forKey_(.8, 'inputRadius')
   
    image = filter.valueForKey_('outputImage')
   
    # the blur makes the extent of the image bigger, so we need to chop it up a bit.
    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
    offset          = cornerRadius/4
   
    nsimg           = image.NSImage()
    size            = nsimg.size()
   
    rect            = ((offset,offset), size)
    pb              = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_(rect, cornerRadius, cornerRadius)
   
    fullSize        = NSMakeSize(size.width + offset * 2, size.height + offset * 2)
   
    newImage        = NSImage.alloc().initWithSize_(fullSize)
    newImage.lockFocus()
   
    imageRect = NSMakeRect(0,0,fullSize.width,fullSize.height)
   
    NSColor.whiteColor().set()
   
    NSRectFill(imageRect)
   
    pb.setClip()
   
    nsimg.drawAtPoint_fromRect_operation_fraction_((offset, offset), NSZeroRect, NSCompositeCopy, 1.0)
   
    newImage.unlockFocus()
   
    return newImage

def main(image):
   
    image       = makeGrayscale(image)
    image       = blurImage(image)
   
    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.0983 seconds