20260406

[NSGraphicsContext] 複数画像のマージ(縦方向)


複数画像のマージ(縦方向)

ダウンロードはこちら
NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

縦方向合成ファイル名順.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006複数選択した画像を縦方向へ結合していく
007
008v1 初回作成
009
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
013use AppleScript version "2.8"
014use framework "Foundation"
015use framework "AppKit"
016use framework "UniformTypeIdentifiers"
017use scripting additions
018property refMe : a reference to current application
019
020
021set appFileManager to refMe's NSFileManager's defaultManager()
022
023#############################
024#ダイアログ
025set appFileManager to refMe's NSFileManager's defaultManager()
026set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
027set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
028set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
029set listUTI to {"public.image"} as list
030set strMes to ("画像ファイルを選んでください") as text
031set strPrompt to ("画像ファイルを選んでください" & return & "ファイル名順に右方向へ結合します") as text
032try
033   tell application "SystemUIServer"
034      activate
035      set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
036   end tell
037on error strErrMes number numErrNo
038   log strErrMes & numErrNo
039   return false
040end try
041if listAliasFilePath is {} then
042   return false
043end if
044
045#############################
046#ピクセルサイズ調査
047
048set numTotalHpx to 0 as integer
049set numMaxWpx to 0 as integer
050
051#最大高さPXの取得 と 幅PXの合計を取得
052repeat with itemAliasFilePath in listAliasFilePath
053   set aliasFilePath to itemAliasFilePath as alias
054   set strFilePath to (POSIX path of aliasFilePath) as text
055   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
056   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
057   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
058   set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
059   set numHpx to ocidImageRep's pixelsHigh()
060   set numWpx to ocidImageRep's pixelsWide()
061   #最大高さの取得
062   if numMaxWpx < numWpx then
063      set numMaxWpx to numWpx as integer
064   end if
065   #幅PXの合計
066   set numTotalHpx to (numTotalHpx + numHpx) as integer
067   #解放
068   set ocidImageRep to (missing value)
069end repeat
070
071log numMaxWpx
072log numTotalHpx
073
074#############################
075#出力画像保存先
076set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask))
077set ocidPicturesDirPathURL to ocidURLsArray's firstObject()
078set ocidSaveDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:("Image_Output") isDirectory:(true)
079#フォルダ作っておく
080set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
081ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
082set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
083#出力画像URL
084set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") as text
085set strSaveFileName to ("" & strDateNo & "-" & numMaxWpx & "x" & numTotalHpx & ".png") as text
086set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
087
088#############################
089#画像作成開始
090set intBPR to (4 * numMaxWpx) as integer
091set ocidAardboardRep to refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numMaxWpx) pixelsHigh:(numTotalHpx) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(32)
092
093
094
095########################################
096#合成
097set numSetHpx to (missing value)
098#▼▼▼saveGraphicsState
099###初期化
100refMe's NSGraphicsContext's saveGraphicsState()
101set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
102(ocidContext's setShouldAntialias:(true))
103(ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh))
104(ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric))
105ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0))
106(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
107########################################
108#ペースト処理
109repeat with itemAliasFilePath in listAliasFilePath
110   #ファイル
111   set aliasFilePath to itemAliasFilePath as alias
112   set strFilePath to (POSIX path of aliasFilePath) as text
113   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
114   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
115   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
116   #NSDATA
117   set ocidOption to (refMe's NSDataReadingMappedIfSafe)
118   set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
119   set ocidReadData to (first item of listResponse)
120   #NSIMAGE
121   set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
122   #NSIMAGEREP
123   set ocidImageRepArray to ocidReadImage's representations()
124   set ocidImageRep to ocidImageRepArray's firstObject()
125   #ピクセルサイズ
126   set numPxW to ocidImageRep's pixelsWide()
127   set numPxH to ocidImageRep's pixelsHigh()
128   set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH)
129   (ocidImageRep's setSize:(ocidImageSizePx))
130   set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH)
131   #ペースト位置
132   set numSetHpx to (numTotalHpx - numPxH) as integer
133   set numSetWpx to (numMaxWpx - numPxW) as integer
134   set ocidDrawRect to refMe's NSRect's NSMakeRect(0, numSetHpx, (numPxW), (numPxH))
135   #   set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH))
136   #元画像をペースト
137   set ocidOption to (refMe's NSCompositingOperationSourceOver)
138   (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value))
139   #次の画像のペースト位置
140   set numTotalHpx to (numSetHpx) as integer
141   #データは一旦解放
142   set ocidImageRep to (missing value)
143   set ocidReadImage to (missing value)
144   set ocidReadData to (missing value)
145end repeat
146
147####画像作成終了
148refMe's NSGraphicsContext's restoreGraphicsState()
149#▲▲▲ restoreGraphicsState
150
151
152########################################
153#PNG変換
154set ocidProperty to refMe's NSMutableDictionary's alloc()'s init()
155ocidProperty's setObject:(false) forKey:(refMe's NSImageInterlaced)
156ocidProperty's setObject:(2.2) forKey:(refMe's NSImageGamma)
157set ocidType to (refMe's NSBitmapImageFileTypePNG)
158set ocidSaveData to (ocidAardboardRep's representationUsingType:(ocidType) |properties|:(ocidProperty))
159
160#保存
161set ocidOption to (refMe's NSDataWritingAtomic)
162set listDone to ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)
163
164########################################
165#保存先を開く
166set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
167set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
168
169return boolDone
170#終了
171
172#############################
173#日付情報の取得
174to doGetDateNo(argFormatStrings)
175   set ocidDate to refMe's NSDate's |date|()
176   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
177   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
178   ocidNSDateFormatter's setDateFormat:(argFormatStrings)
179   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
180   set strDateAndTime to ocidDateAndTime as text
181   return strDateAndTime
182end doGetDateNo
183
184
AppleScriptで生成しました