twitter用画像生成 9種類の画像の合成
画像の合成 9つの画像を4つにマージして投稿用の画像を生成するhttps://t.co/bwFI7A5oTQ pic.twitter.com/FU8roWy7FH
— QuickTimer (@floe_ice) August 6, 2024
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # 試してみたい場合は |
005 | # https://quicktimer.cocolog-nifty.com/icefloe/files/tw9to4images.zip |
006 | # ↑をダウンロードして試してください |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "UniformTypeIdentifiers" |
012 | use framework "AppKit" |
013 | use scripting additions |
014 | |
015 | property refMe : a reference to current application |
016 | |
017 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | |
019 | #################### |
020 | #基本パス |
021 | set aliasPathToMe to (path to me) as alias |
022 | tell application "Finder" |
023 | set aliasContainerDirPath to (container of aliasPathToMe) as alias |
024 | end tell |
025 | #コンテナ |
026 | set strContainerDirPath to (POSIX path of aliasContainerDirPath) as text |
027 | set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath) |
028 | set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath() |
029 | set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:false) |
030 | #保存先フォルダ |
031 | set ocidSaveDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("FinishImage") isDirectory:(true) |
032 | #マテリアルフォルダ |
033 | set ocidMaterialDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("01_Material") isDirectory:(true) |
034 | #メインイメージフォルダ |
035 | set ocidMainDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("02_MainImage") isDirectory:(true) |
036 | |
037 | #################### |
038 | #コンテンツ収集 1階層のみ |
039 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
040 | set ocidKeyArray to refMe's NSArray's arrayWithArray:({(refMe's NSURLPathKey), (refMe's NSURLIsDirectoryKey), (refMe's NSURLIsSymbolicLinkKey)}) |
041 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidMaterialDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error| :(reference)) |
042 | if (item 2 of listResponse) = (missing value) then |
043 | set ocidSubPathArray to (item 1 of listResponse) |
044 | else if (item 2 of listResponse) ≠ (missing value) then |
045 | set strErrorNO to (item 2 of listResponse)'s code() as text |
046 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
047 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
048 | return "エラーしました" & strErrorNO & strErrorMes |
049 | end if |
050 | #ソート |
051 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:") |
052 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
053 | set ocidSortedArray to ocidSubPathArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
054 | #素材のURL |
055 | (* |
056 | set ocidTopImageURL to ocidMaterialDirPathURL's URLByAppendingPathComponent:("1@1920x1080.png") isDirectory:(false) |
057 | set ocidBottomImageURL to ocidMaterialDirPathURL's URLByAppendingPathComponent:("2@1920x1080.png") isDirectory:(false) |
058 | *) |
059 | # メインになるイメージ |
060 | set ocidMidImageURL to ocidMainDirPathURL's URLByAppendingPathComponent:("main@1920x1080.jpg") isDirectory:(false) |
061 | |
062 | #################### |
063 | #4回繰り返し |
064 | set numCntImageNO to 0 as integer |
065 | repeat with itemNo from 1 to (4) by 1 |
066 | #上下画像の取得 |
067 | set numTopImageNO to (numCntImageNO) as integer |
068 | set numBottomImageNo to (numTopImageNO + 1) as integer |
069 | #URLに |
070 | set ocidTopImageURL to (ocidSortedArray's objectAtIndex:(numTopImageNO)) |
071 | set ocidBottomImageURL to (ocidSortedArray's objectAtIndex:(numBottomImageNo)) |
072 | set numCntImageNO to (numBottomImageNo + 1) as integer |
073 | #保存ファイルURLを生成 |
074 | set numSaveImageNo to itemNo as integer |
075 | set strSaveFileName to (numSaveImageNo & "@960x1620.png") as text |
076 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
077 | |
078 | #################### |
079 | #出力用のマージ画像 |
080 | set numSetPxWidth to (960) as integer |
081 | set numSetPxHeight to (1620) as integer |
082 | set ocidSaveImagePixelsSize to refMe's NSMakeSize(numSetPxWidth, numSetPxHeight) |
083 | # samplesPerPixel |
084 | set intSPP to 4 as integer |
085 | # bitsPerSample |
086 | set intBPS to 8 as integer |
087 | # bytesPerRow |
088 | set intBPR to 0 as integer |
089 | # bitsPerPixel |
090 | set intBPP to 32 as integer |
091 | # RGB系のカラースペース |
092 | set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace |
093 | # アルファあり |
094 | set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat |
095 | #出力ピクセルサイズのブランクイメージ |
096 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetPxWidth) pixelsHigh:(numSetPxHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP)) |
097 | #################### |
098 | #ArtBord |
099 | refMe's NSGraphicsContext's saveGraphicsState() |
100 | #Context |
101 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
102 | #生成された画像でNSGraphicsContext初期化 |
103 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
104 | #色を個別に指定する場合 値は0が暗 1が明 |
105 | set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0)) |
106 | ocidSetColor's |set|() |
107 | #画像生成 |
108 | set ocidSaveImageRect to refMe's NSMakeRect(0, 0, numSetPxWidth, (numSetPxHeight)) |
109 | refMe's NSRectFill(ocidSaveImageRect) |
110 | ####画像作成終了 |
111 | refMe's NSGraphicsContext's restoreGraphicsState() |
112 | |
113 | #################### |
114 | #上部画像読み込み |
115 | set ocidTopImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidTopImageURL)) |
116 | set ocidImageSize to ocidTopImage's |size|() |
117 | set numPtWidth to ocidImageSize's width |
118 | set numPtHeigh to ocidImageSize's height |
119 | set ocidTopImageRepArray to ocidTopImage's representations() |
120 | set ocidTopImageRep to ocidTopImageRepArray's firstObject() |
121 | set numPixelsWidth to ocidTopImageRep's pixelsWide() |
122 | set numPixelsHeight to ocidTopImageRep's pixelsHigh() |
123 | set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight) |
124 | (ocidTopImageRep's setSize:(ocidPixelsSize)) |
125 | |
126 | #################### |
127 | #上部マージ |
128 | refMe's NSGraphicsContext's saveGraphicsState() |
129 | #Context |
130 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
131 | #NSGraphicsContext初期化 |
132 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
133 | #NSCompositeSourceOver |
134 | set ocidFromRect to refMe's NSMakeRect(0, 0, numPixelsWidth, numPixelsHeight) |
135 | set ocidDrawRect to refMe's NSMakeRect(0, 1080, 960, 540) |
136 | # |
137 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
138 | # |
139 | (ocidTopImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value)) |
140 | #画像作成終了 |
141 | refMe's NSGraphicsContext's restoreGraphicsState() |
142 | |
143 | #################### |
144 | #下部部画像読み込み |
145 | set ocidBottomImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidBottomImageURL)) |
146 | set ocidImageSize to ocidBottomImage's |size|() |
147 | set numPtWidth to ocidImageSize's width |
148 | set numPtHeigh to ocidImageSize's height |
149 | set ocidBottomImageRepArray to ocidBottomImage's representations() |
150 | set ocidBottomImageRep to ocidBottomImageRepArray's firstObject() |
151 | set numPixelsWidth to ocidBottomImageRep's pixelsWide() |
152 | set numPixelsHeight to ocidBottomImageRep's pixelsHigh() |
153 | set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight) |
154 | (ocidBottomImageRep's setSize:(ocidPixelsSize)) |
155 | |
156 | #################### |
157 | #下部マージ |
158 | refMe's NSGraphicsContext's saveGraphicsState() |
159 | #Context |
160 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
161 | #NSGraphicsContext初期化 |
162 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
163 | #NSCompositeSourceOver |
164 | set ocidFromRect to refMe's NSMakeRect(0, 0, numPixelsWidth, numPixelsHeight) |
165 | set ocidDrawRect to refMe's NSMakeRect(0, 0, 960, 540) |
166 | # |
167 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
168 | # |
169 | (ocidBottomImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value)) |
170 | #画像作成終了 |
171 | refMe's NSGraphicsContext's restoreGraphicsState() |
172 | |
173 | #################### |
174 | #メイン画像読み込み |
175 | set ocidMidImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidMidImageURL)) |
176 | set ocidImageSize to ocidMidImage's |size|() |
177 | set numPtWidth to ocidImageSize's width |
178 | set numPtHeigh to ocidImageSize's height |
179 | set ocidMidImageRepArray to ocidMidImage's representations() |
180 | set ocidMidImageRep to ocidMidImageRepArray's firstObject() |
181 | set numPixelsWidth to ocidMidImageRep's pixelsWide() |
182 | set numPixelsHeight to ocidMidImageRep's pixelsHigh() |
183 | set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight) |
184 | (ocidMidImageRep's setSize:(ocidPixelsSize)) |
185 | |
186 | #################### |
187 | #メインマージ |
188 | refMe's NSGraphicsContext's saveGraphicsState() |
189 | #Context |
190 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
191 | #NSGraphicsContext初期化 |
192 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
193 | #NSCompositeSourceOver |
194 | #メインのイメージのどのRECTを取得するか? |
195 | if itemNo is 1 then |
196 | #1 |
197 | set ocidFromRect to refMe's NSMakeRect(0, 540, 960, 540) |
198 | else if itemNo is 2 then |
199 | #2 |
200 | set ocidFromRect to refMe's NSMakeRect(960, 540, 960, 540) |
201 | else if itemNo is 3 then |
202 | #3 |
203 | set ocidFromRect to refMe's NSMakeRect(0, 0, 960, 540) |
204 | else if itemNo is 4 then |
205 | #4 |
206 | set ocidFromRect to refMe's NSMakeRect(960, 0, 960, 540) |
207 | end if |
208 | # |
209 | set ocidDrawRect to refMe's NSMakeRect(0, 540, 960, 540) |
210 | # |
211 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
212 | # |
213 | (ocidMidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value)) |
214 | #画像作成終了 |
215 | refMe's NSGraphicsContext's restoreGraphicsState() |
216 | |
217 | #################### |
218 | #保存オプション |
219 | set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
220 | (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
221 | (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
222 | ##保存 |
223 | set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
224 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) atomically:true) |
225 | |
226 | |
227 | |
228 | end repeat |
229 | |
230 | |
231 | #################### |
232 | # |
233 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
234 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
235 | |
236 | |
237 | |
238 | |
AppleScriptで生成しました |
| 固定リンク