Core ImageのCIFilterから、autoAdjustmentFiltersを呼び出して、いい感じにKeynote上とかで自動画像調整を行うのと同様に、画像に対して自動調整を行うAppleScriptです。
KeynoteやPages上の画像の自動補正機能は、大当たりすることもなければ大はずしすることもない、なかなかお得な機能です。
ちょっと、Finder上で選択した画像にひととおり自動補正をかけてお茶をにごしたい。
そんな機能が存在しないかと調べていたのですが、autoAdjustmentFiltersの存在は知っていたものの、いろいろ試しては、
current application’s CIImage’s autoAdjustmentFilters()
とか、
current application’s CIFilter’s autoAdjustmentFilters()
などと間違った記述を行なってmissing valueが返ってきては、首をひねりまくっていました。
Googleの検索エンジンで探してもなかなかサンプルに行き当たらないあたり、みんな苦労しているんじゃないかと疑っていますが、これは、
CIImage画像に対してautoAdjustmentFilters()を実行すると、自動調整用のCIFilterにパラメータが設定された状態でarrayに入って返ってくるのでした(これは、わからないぞ)。
半信半疑で実行してみたら、なんとなくそれっぽく自動調整された画像がデスクトップに出力されます。
AppleScript名:CoreImageで指定画像をautoAdjustmentFilters.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/01/21 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.8" use framework "Foundation" use framework "AppKit" use framework "CoreImage" use scripting additions property CIFilter : a reference to current application’s CIFilter property NSUUID : a reference to current application’s NSUUID property |NSURL| : a reference to current application’s |NSURL| property CIImage : a reference to current application’s CIImage property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSPNGFileType : a reference to current application’s NSPNGFileType property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep set imgPath to POSIX path of (choose file) set aNSImage to makeNSImageFromPOSIXpath(imgPath) of me set bImgRes to autoFiltersForNSImage(aNSImage) of me if bImgRes = false then return set outPath to retUUIDfilePath(POSIX path of (path to desktop), "png") of me set sRes to saveNSImageAtPathAsPNG(bImgRes, outPath) of me on autoFiltersForNSImage(aNSImage) set aCIImage to convNSImageToCIimage(aNSImage) of me set filterList to aCIImage’s autoAdjustmentFilters if filterList = missing value then return false repeat with i in filterList set aFilter to contents of i (aFilter’s setValue:(aCIImage) forKey:"inputImage") set aOutImage to (aFilter’s valueForKey:"outputImage") copy aOutImage to aCIImage end repeat set outNSImage to convCIimageToNSImage(aOutImage) of me return outNSImage end autoFiltersForNSImage on retUUIDfilePath(aPath, aEXT) set aUUIDstr to (NSUUID’s UUID()’s UUIDString()) as string set aPath to ((NSString’s stringWithString:aPath)’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:aEXT return aPath end retUUIDfilePath on convCIimageToNSImage(aCIImage) set aRep to NSBitmapImageRep’s alloc()’s initWithCIImage:aCIImage set tmpSize to aRep’s |size|() set newImg to NSImage’s alloc()’s initWithSize:tmpSize newImg’s addRepresentation:aRep return newImg end convCIimageToNSImage on convNSImageToCIimage(aNSImage) set tiffDat to aNSImage’s TIFFRepresentation() set aRep to NSBitmapImageRep’s imageRepWithData:tiffDat set newImg to CIImage’s alloc()’s initWithBitmapImageRep:aRep return newImg end convNSImageToCIimage on makeNSImageFromAlias(anAlias) set imgPath to (POSIX path of anAlias) set aURL to (|NSURL|’s fileURLWithPath:(imgPath)) return (NSImage’s alloc()’s initWithContentsOfURL:aURL) end makeNSImageFromAlias on makeNSImageFromPOSIXpath(aPOSIX) set aURL to (|NSURL|’s fileURLWithPath:(aPOSIX)) return (NSImage’s alloc()’s initWithContentsOfURL:aURL) end makeNSImageFromPOSIXpath –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –true/false end saveNSImageAtPathAsPNG |
More from my site
(Visited 4 times, 4 visits today)