How to apply a CIFilter to an image in Swift

import Foundation
import CoreImage
import AppKit
let nsImage = NSImage(contentsOfFile: "/Users/jim/Lenna.png")!
let cgImage = nsImage.cgImage(forProposedRect: nil, context: nil, hints: [:])!
let inputImage = CIImage(cgImage: cgImage)
let filter = CIFilter(name: "CISepiaTone")!
filter.setValue(inputImage, forKey: kCIInputImageKey)
filter.setValue(1, forKey: kCIInputIntensityKey)
let outputImage = filter.outputImage!
let rep = NSBitmapImageRep(ciImage: outputImage)
let imageData = rep.representation(using: NSBitmapImageFileType.PNG, properties: [:])!
try! imageData.write(to: URL.init(string: "file:/Users/jim/output.png")!, options: NSData.WritingOptions.atomic)
Tagged .

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.