20260406

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


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

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

【Safari・FireFox用Script Editorで開く】 |

複数画像ファイルのマージ横方向R2L合成.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006複数選択した画像を横方向へ結合していく
007
008v1 初回作成
009v1.1 起点の誤りを修正
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 numMaxHpx to 0 as integer
049set numTotalWpx 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 numMaxHpx < numHpx then
063      set numMaxHpx to numHpx as integer
064   end if
065   #幅PXの合計
066   set numTotalWpx to (numTotalWpx + numWpx) as integer
067   #解放
068   set ocidImageRep to (missing value)
069end repeat
070
071log numMaxHpx
072log numTotalWpx
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 & "-" & numTotalWpx & "x" & numMaxHpx & ".png") as text
086set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
087
088#############################
089#画像作成開始
090set intBPR to (4 * numTotalWpx) as integer
091set 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)
092
093
094
095########################################
096#合成
097set numSetWpx 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   if numSetWpx = (missing value) then
133      set numSetWpx to (numTotalWpx - numPxW) as integer
134   else
135      set numSetWpx to (numTotalWpx - numPxW) as integer
136   end if
137   set numTotalWpx to numSetWpx as integer
138   set numSetHpx to (numMaxHpx - numPxH) as integer
139   set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH))
140   #元画像をペースト
141   set ocidOption to (refMe's NSCompositingOperationSourceOver)
142   (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value))
143   #次の画像のペースト位置
144   set numSetWpx to (numSetWpx - numPxW) as integer
145   #データは一旦解放
146   set ocidImageRep to (missing value)
147   set ocidReadImage to (missing value)
148   set ocidReadData to (missing value)
149end repeat
150
151####画像作成終了
152refMe's NSGraphicsContext's restoreGraphicsState()
153#▲▲▲ restoreGraphicsState
154
155
156########################################
157#PNG変換
158set ocidProperty to refMe's NSMutableDictionary's alloc()'s init()
159ocidProperty's setObject:(false) forKey:(refMe's NSImageInterlaced)
160ocidProperty's setObject:(2.2) forKey:(refMe's NSImageGamma)
161set ocidType to (refMe's NSBitmapImageFileTypePNG)
162set ocidSaveData to (ocidAardboardRep's representationUsingType:(ocidType) |properties|:(ocidProperty))
163
164#保存
165set ocidOption to (refMe's NSDataWritingAtomic)
166set listDone to ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)
167
168########################################
169#保存先を開く
170set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
171set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
172
173return boolDone
174#終了
175
176#############################
177#日付情報の取得
178to doGetDateNo(argFormatStrings)
179   set ocidDate to refMe's NSDate's |date|()
180   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
181   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
182   ocidNSDateFormatter's setDateFormat:(argFormatStrings)
183   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
184   set strDateAndTime to ocidDateAndTime as text
185   return strDateAndTime
186end doGetDateNo
187
188
AppleScriptで生成しました


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

横方向合成ファイル名順.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で生成しました