単色画像に色を指定して塗りつぶしを行うAppleScriptです。
▲Original Image
▲Result Image
AppleScript名:単色画像(アルファ値必要)に色を指定して塗りつぶし |
— Created 2017-04-24 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "QuartzCore" –http://piyocast.com/as/archives/4615 set aFile to POSIX path of (choose file of type "public.image") set aColor to makeNSColorFromRGBA255val(0, 0, 255, 255) of me set aColoredImage to fillColorWithImage(aFile, aColor) of me set aDesktopPath to ((current application’s NSProcessInfo’s processInfo()’s environment()’s objectForKey:("HOME"))’s stringByAppendingString:"/Desktop/") set savePath to (aDesktopPath’s stringByAppendingString:((current application’s NSString’s stringWithString:"testOverlay")’s stringByAppendingString:".png")) set fRes to saveNSImageAtPathAsPNG(aColoredImage, savePath) of me on fillColorWithImage(aFile, aColor) set anImage to (current application’s NSImage’s alloc()’s initWithContentsOfFile:aFile) set aSize to anImage’s |size|() set aWidth to (aSize’s width) set aHeight to (aSize’s height) set colordImage to makeNSImageWithFilledWithColor(aWidth, aHeight, aColor) of me colordImage’s lockFocus() anImage’s drawAtPoint:{0, 0} fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeDestinationIn) fraction:1.0 colordImage’s unlockFocus() return colordImage end fillColorWithImage –指定サイズの画像を作成し、指定色で塗ってファイル書き出し on makeNSImageWithFilledWithColor(aWidth, aHeight, fillColor) set anImage to current application’s NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight)) anImage’s lockFocus() — set theRect to {{x:0, y:0}, {height:aHeight, width:aWidth}} set theNSBezierPath to current application’s NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect — fillColor’s |set|() –色設定 theNSBezierPath’s fill() –ぬりつぶし — anImage’s unlockFocus() — return anImage end makeNSImageWithFilledWithColor on makeNSColorFromRGBA255val(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer) set aRedCocoa to (redValue / 255) as real set aGreenCocoa to (greenValue / 255) as real set aBlueCocoa to (blueValue / 255) as real set aAlphaCocoa to (alphaValue / 255) as real set aColor to current application’s NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBA255val –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep set pathString to current application’s NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG |