[CoreImage]チェキサイズのフレームを画像に合成する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | #com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "UniformTypeIdentifiers" |
009 | use framework "AppKit" |
010 | use framework "CoreImage" |
011 | use framework "CoreGraphics" |
012 | use framework "QuartzCore" |
013 | |
014 | use scripting additions |
015 | property refMe : a reference to current application |
016 | |
017 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | set strName to (name of current application) as text |
019 | if strName is "osascript" then |
020 | tell application "Finder" to activate |
021 | else |
022 | tell current application to activate |
023 | end if |
024 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
025 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
026 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
027 | # |
028 | set listUTI to {"public.image"} as list |
029 | set strMes to ("ファイルを選んでください") as text |
030 | set strPrompt to ("ファイルを選んでください") as text |
031 | try |
032 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list |
033 | on error |
034 | log "エラーしました" |
035 | return "エラーしました" |
036 | end try |
037 | |
038 | repeat with itemAliasFilePath in listAliasFilePath |
039 | #入力 |
040 | set strFilePath to (POSIX path of itemAliasFilePath) as text |
041 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
042 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
043 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
044 | #拡張子とったベースファイルパス |
045 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
046 | #保存パス |
047 | set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("Frame.png")) |
048 | |
049 | ##NSDATAに読み込む |
050 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
051 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)) |
052 | if (item 2 of listResponse) = (missing value) then |
053 | log "正常処理" |
054 | set ocidReadData to (item 1 of listResponse) |
055 | else if (item 2 of listResponse) ≠ (missing value) then |
056 | set strErrorNO to (item 2 of listResponse)'s code() as text |
057 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
058 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
059 | return "エラーしました" & strErrorNO & strErrorMes |
060 | end if |
061 | |
062 | #NSDATAをCIimageに読み込み |
063 | set ocidCIimage to (refMe's CIImage's alloc()'s initWithData:(ocidReadData)) |
064 | #Rectを取り出して |
065 | set listReadRect to ocidCIimage's extent() |
066 | #ピクセルサイズ |
067 | set numReadW to (item 1 of (item 2 of listReadRect)) as integer |
068 | set numReadH to (item 2 of (item 2 of listReadRect)) as integer |
069 | #縦横比を計算 Wで1.6 miniで 0.74 |
070 | set numWHscale to numReadW / numReadH as number |
071 | log numWHscale |
072 | # |
073 | if numReadW > numReadH then |
074 | #横型 |
075 | #Wide |
076 | set numW to 612 as integer |
077 | set numH to 488 as integer |
078 | set ocidMaskRect to refMe's NSRect's NSMakeRect(28, 108, 556, 352) |
079 | set numCropInX to (item 1 of (item 1 of ocidMaskRect)) as integer |
080 | set numCropInY to (item 2 of (item 1 of ocidMaskRect)) as integer |
081 | set numCropInW to (item 1 of (item 2 of ocidMaskRect)) as integer |
082 | set numCropInH to (item 2 of (item 2 of ocidMaskRect)) as integer |
083 | #リサイズ用のスケール |
084 | if numWHscale > 1.59 then |
085 | set numScale to (numCropInH / numReadH) as number |
086 | else |
087 | set numScale to (numCropInW / numReadW) as number |
088 | end if |
089 | else if numReadW < numReadH then |
090 | #縦型 |
091 | #mini |
092 | set numW to 306 as integer |
093 | set numH to 488 as integer |
094 | set ocidMaskRect to refMe's NSRect's NSMakeRect(23, 108, 260, 352) |
095 | set numCropInX to (item 1 of (item 1 of ocidMaskRect)) as integer |
096 | set numCropInY to (item 2 of (item 1 of ocidMaskRect)) as integer |
097 | set numCropInW to (item 1 of (item 2 of ocidMaskRect)) as integer |
098 | set numCropInH to (item 2 of (item 2 of ocidMaskRect)) as integer |
099 | if numWHscale > 0.74 then |
100 | set numScale to (numCropInH / numReadH) as number |
101 | else |
102 | set numScale to (numCropInW / numReadW) as number |
103 | end if |
104 | |
105 | else if numWHscale = 1 then |
106 | #正方形 |
107 | #SQ |
108 | set numW to 408 as integer |
109 | set numH to 488 as integer |
110 | set ocidMaskRect to refMe's NSRect's NSMakeRect(28, 108, 352, 352) |
111 | set numCropInW to (item 1 of (item 2 of ocidMaskRect)) as integer |
112 | set numCropInH to (item 2 of (item 2 of ocidMaskRect)) as integer |
113 | #リサイズ用のスケール短編合わせ |
114 | set numScale to (numCropInW / numReadW) as number |
115 | end if |
116 | ########################## |
117 | #ペーストするフレーム画像作成 |
118 | #カラーICC |
119 | set strIccFilePath to ("/System/Library/ColorSync/Profiles/sRGB Profile.icc") as text |
120 | set ocidIccFilePathStr to (refMe's NSString's stringWithString:(strIccFilePath)) |
121 | set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath |
122 | set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:(false)) |
123 | set ocidProfileData to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL)) |
124 | #セットするRECT |
125 | set ocidDrawRect to refMe's NSRect's NSMakeRect(0, 0, numW, numH) |
126 | # RGB系のカラースペース |
127 | set ocidColorSpaceName to (refMe's NSCalibratedRGBColorSpace) |
128 | # アルファあり |
129 | set ocidBitmapFormat to (refMe's NSAlphaFirstBitmapFormat) |
130 | #アートボード |
131 | set ocidFrameRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numW) pixelsHigh:(numH) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(0) bitsPerPixel:(32)) |
132 | #カラーを定義 |
133 | #白背景 |
134 | set ocidBackgroundColor to (refMe's NSColor's colorWithSRGBRed:(1) green:(1) blue:(1) alpha:(1)) |
135 | #抜きのアルファ |
136 | set ocidAlphaColor to (refMe's NSColor's colorWithSRGBRed:(1) green:(1) blue:(1) alpha:(0)) |
137 | # |
138 | #【NSGraphicsContext's】▼ |
139 | refMe's NSGraphicsContext's saveGraphicsState() |
140 | #Context 選択した画像を読み込んで |
141 | set ocidSetImageContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidFrameRep)) |
142 | (ocidSetImageContext's setShouldAntialias:(true)) |
143 | (ocidSetImageContext's setImageInterpolation:(refMe's NSImageInterpolationHigh)) |
144 | (ocidSetImageContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric)) |
145 | #生成された画像でNSGraphicsContext初期化 |
146 | (refMe's NSGraphicsContext's setCurrentContext:(ocidSetImageContext)) |
147 | |
148 | #【1】RGB値で背景色=白をセットして |
149 | ocidBackgroundColor's |set|() |
150 | #指定した色で背景を塗る |
151 | refMe's NSRectFill(ocidDrawRect) |
152 | |
153 | #マスクパス角丸20PX |
154 | set ocidMaskPath to (refMe's NSBezierPath's bezierPathWithRoundedRect:(ocidMaskRect) xRadius:(4) yRadius:(4)) |
155 | #【2】マスクをセット |
156 | ocidMaskPath's addClip() |
157 | |
158 | #【3】アルファ=透過をマスク内に指定する |
159 | ocidAlphaColor's |set|() |
160 | #透過色をマスク範囲内に指定 |
161 | refMe's NSRectFill(ocidMaskRect) |
162 | |
163 | #処理終了 |
164 | refMe's NSGraphicsContext's restoreGraphicsState() |
165 | #【NSGraphicsContext's】▲ |
166 | #出来上がったフレームイメージをCIimageにしておく |
167 | set ocidFrameCiImage to (refMe's CIImage's alloc()'s initWithBitmapImageRep:(ocidFrameRep)) |
168 | |
169 | ########################## |
170 | #リサイズ定義 |
171 | set appTransform to refMe's CGAffineTransformMakeScale(numScale, numScale) |
172 | #リサイズ |
173 | set ocidResizeImage to (ocidCIimage's imageByApplyingTransform:(appTransform)) |
174 | #リサイズ後のピクセルサイズ |
175 | set listResizedRect to ocidResizeImage's extent() |
176 | set numResizedW to (item 1 of (item 2 of listResizedRect)) as integer |
177 | set numResizedH to (item 2 of (item 2 of listResizedRect)) as integer |
178 | log numResizedW |
179 | log numResizedH |
180 | #CROPのRECTを計算 |
181 | if numReadW > numReadH then |
182 | set numSetX to ((numResizedW - numCropInW) / 2) as integer |
183 | set numSetY to ((numResizedH - numCropInH) / 2) as integer |
184 | set ocidImageRect to refMe's NSRect's NSMakeRect(numSetX, numSetY, numCropInW, numCropInH) |
185 | set ocidImageSize to refMe's NSSize's NSMakeSize(numW, numH) |
186 | else if numReadW < numReadH then |
187 | set numSetX to ((numResizedW - numCropInW) / 2) as integer |
188 | set numSetY to ((numResizedH - numCropInH) / 2) as integer |
189 | set ocidImageRect to refMe's NSRect's NSMakeRect(numSetX, numSetY, numCropInW, numCropInH) |
190 | set ocidImageSize to refMe's NSSize's NSMakeSize(numW, numH) |
191 | else if numReadW = numReadH then |
192 | set ocidImageRect to refMe's NSRect's NSMakeRect(0, 0, 408, 488) |
193 | set ocidImageSize to refMe's NSSize's NSMakeSize(numW, numH) |
194 | end if |
195 | ################ |
196 | #元画像をフレームサイズでCROP |
197 | set ocidCgRect to refMe's NSRectToCGRect(ocidImageRect) |
198 | set ocidCropRect to (refMe's CIVector's vectorWithCGRect:(ocidCgRect)) |
199 | #フィルター定義 |
200 | set appCropFilter to (refMe's CIFilter's filterWithName:("CICrop")) |
201 | (appCropFilter's setValue:(ocidResizeImage) forKey:(refMe's kCIInputImageKey)) |
202 | (appCropFilter's setValue:(ocidCropRect) forKey:("inputRectangle")) |
203 | #フィルタ実行 |
204 | set ocidCropImage to appCropFilter's outputImage() |
205 | |
206 | ################ |
207 | # |
208 | set numSetTransX to numSetX - numCropInX |
209 | set numSetTransY to (numSetY - numCropInY) |
210 | #負の数になったら整数にする |
211 | if numSetTransY < 0 then |
212 | set numSetTransY to (((numSetTransY) ^ 2) ^ 0.5) as number |
213 | end if |
214 | set appTranslation to refMe's CGAffineTransformMakeTranslation(-numSetTransX, numSetTransY) |
215 | set appTopImage to (ocidCropImage's imageByApplyingTransform:(appTranslation)) |
216 | # |
217 | set appFilter to (refMe's CIFilter's filterWithName:("CISourceOverCompositing")) |
218 | (appFilter's setValue:(ocidFrameCiImage) forKey:(refMe's kCIInputImageKey)) |
219 | (appFilter's setValue:(appTopImage) forKey:(refMe's kCIInputBackgroundImageKey)) |
220 | set ocidCreationCiImage to appFilter's outputImage() |
221 | #ImageRepに変換 |
222 | set ocidSaveImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithCIImage:(ocidCreationCiImage)) |
223 | |
224 | ################ |
225 | #保存オプション |
226 | set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
227 | (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
228 | (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
229 | #保存 |
230 | set ocidNSInlineData to (ocidSaveImageRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
231 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) atomically:true) |
232 | |
233 | |
234 | |
235 | end repeat |
236 | |
237 | |
238 | return |
AppleScriptで生成しました |
| 固定リンク