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
010com.cocolog-nifty.quicktimer.icefloe *)
011----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
012use AppleScript version "2.8"
013use framework "Foundation"
014use framework "AppKit"
015use framework "UniformTypeIdentifiers"
016use scripting additions
017property refMe : a reference to current application
018
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021
022#############################
023#ダイアログ
024set appFileManager to refMe's NSFileManager's defaultManager()
025set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
026set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
027set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
028set listUTI to {"public.image"} as list
029set strMes to ("画像ファイルを選んでください") as text
030set strPrompt to ("画像ファイルを選んでください" & return & "ファイル名順に右方向へ結合します") as text
031try
032   tell application "SystemUIServer"
033      activate
034      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
035   end tell
036on error strErrMes number numErrNo
037   log strErrMes & numErrNo
038   return false
039end try
040if listAliasFilePath is {} then
041   return false
042end if
043
044#############################
045#ピクセルサイズ調査
046
047set numMaxHpx to 0 as integer
048set numTotalWpx to 0 as integer
049
050#最大高さPXの取得 と 幅PXの合計を取得
051repeat with itemAliasFilePath in listAliasFilePath
052   set aliasFilePath to itemAliasFilePath as alias
053   set strFilePath to (POSIX path of aliasFilePath) as text
054   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
055   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
056   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
057   set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
058   set numHpx to ocidImageRep's pixelsHigh()
059   set numWpx to ocidImageRep's pixelsWide()
060   #最大高さの取得
061   if numMaxHpx < numHpx then
062      set numMaxHpx to numHpx as integer
063   end if
064   #幅PXの合計
065   set numTotalWpx to (numTotalWpx + numWpx) as integer
066   #解放
067   set ocidImageRep to (missing value)
068end repeat
069
070log numMaxHpx
071log numTotalWpx
072
073#############################
074#出力画像保存先
075set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask))
076set ocidPicturesDirPathURL to ocidURLsArray's firstObject()
077set ocidSaveDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:("Image_Output") isDirectory:(true)
078#フォルダ作っておく
079set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
080ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
081set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
082#出力画像URL
083set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") as text
084set strSaveFileName to ("" & strDateNo & "-" & numTotalWpx & "x" & numMaxHpx & ".png") as text
085set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
086
087#############################
088#画像作成開始
089set intBPR to (4 * numTotalWpx) as integer
090set ocidAardboardRep to refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numTotalWpx) pixelsHigh:(numMaxHpx) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(32)
091
092
093
094########################################
095#合成
096set numSetWpx to (missing value)
097#▼▼▼saveGraphicsState
098###初期化
099refMe's NSGraphicsContext's saveGraphicsState()
100set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
101(ocidContext's setShouldAntialias:(true))
102(ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh))
103(ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric))
104ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0))
105(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
106########################################
107#ペースト処理
108repeat with itemAliasFilePath in listAliasFilePath
109   #ファイル
110   set aliasFilePath to itemAliasFilePath as alias
111   set strFilePath to (POSIX path of aliasFilePath) as text
112   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
113   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
114   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
115   #NSDATA
116   set ocidOption to (refMe's NSDataReadingMappedIfSafe)
117   set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
118   set ocidReadData to (first item of listResponse)
119   #NSIMAGE
120   set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
121   #NSIMAGEREP
122   set ocidImageRepArray to ocidReadImage's representations()
123   set ocidImageRep to ocidImageRepArray's firstObject()
124   #ピクセルサイズ
125   set numPxW to ocidImageRep's pixelsWide()
126   set numPxH to ocidImageRep's pixelsHigh()
127   set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH)
128   (ocidImageRep's setSize:(ocidImageSizePx))
129   set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH)
130   #ペースト位置
131   if numSetWpx = (missing value) then
132      set numSetWpx to 0 as integer
133   end if
134   set numSetHpx to (numMaxHpx - numPxH) as integer
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 numSetWpx to (numSetWpx + numPxW) 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で生成しました