20260618

画像の結合

画像の結合

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

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

IMG_Merge.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 右から左への起点の誤りを訂正
010v1.2 ダイアログメッセージの誤りを修正
011
012
013com.cocolog-nifty.quicktimer.icefloe *)
014----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
015use AppleScript version "2.8"
016use framework "Foundation"
017use framework "AppKit"
018use framework "UniformTypeIdentifiers"
019use scripting additions
020
021property refMe : a reference to current application
022property listAliasFilePath : a reference to {} as list
023########################
024# RUN
025
026# on run {listAliasFilePath}
027
028(*
029   *)
030on run
031   if (count of listAliasFilePath) = 0 then
032      set refResponse to doChooseFile()
033      if refResponse is false then
034         return false
035      else
036         set listAliasFilePath to refResponse as list
037      end if
038   end if
039   
040   set boolDone to (open listAliasFilePath)
041   return boolDone
042end run
043
044########################
045# OPEN
046on open listAliasFilePath
047   ########
048   set refResponse to doChooseList()
049   #      0"【左→右】横方向(L2R)" then
050   #      1 "【右→左】横方向(R2L)" 
051   #      2 "【画像合成】縦方向"
052   
053   if refResponse = 0 then
054      set numDoMakeImage to refResponse as integer
055   else if refResponse = 1 then
056      set numDoMakeImage to refResponse as integer
057   else if refResponse = 2 then
058      set numDoMakeImage to refResponse as integer
059      
060   else if refResponse is false then
061      return false
062   end if
063   ########
064   set listWhiteList to {"public.image", "public.png", "public.tiff", "public.jpeg", "org.webmproject.webp", "public.heic", "public.heif", "public.avci", "com.microsoft.bmp", "com.adobe.photoshop-image"} as list
065   set listImageFilePath to {} as list
066   repeat with itemAliasFilePath in listAliasFilePath
067      set aliasItemFilePath to itemAliasFilePath as alias
068      set recordInfoFor to (info for aliasItemFilePath) as record
069      set strUTI to (type identifier of recordInfoFor) as text
070      if listWhiteList contains strUTI then
071         set end of listImageFilePath to aliasItemFilePath
072      end if
073   end repeat
074   if listImageFilePath is not {} then
075      set boolDone to doMakeImage(listImageFilePath, numDoMakeImage)
076   end if
077   
078   
079   return boolDone
080end open
081
082
083#############################
084#本処理
085
086on doMakeImage(listImageFilePath, numDoMakeImage)
087   #保存先
088   set ocidSaveDirPathURL to doMakeSaveDir()
089   
090   #画像ピクセルサイズ
091   set listResponse to doGetImageSize(listImageFilePath)
092   set numMaxWpx to (first item of listResponse) as integer
093   set numMaxHpx to (second item of listResponse) as integer
094   set numTotalWpx to (third item of listResponse) as integer
095   set numTotalHpx to (last item of listResponse) as integer
096   #
097   set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") as text
098   #############################
099   #画像作成開始
100   if numDoMakeImage = 0 then
101      #保存先
102      set strSaveFileName to ("" & strDateNo & "-" & numTotalWpx & "x" & numMaxHpx & ".png") as text
103      set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
104      #処理開始
105      set intBPR to (4 * numTotalWpx) as integer
106      set 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)
107      #合成
108      set numSetWpx to (missing value)
109      #▼▼▼saveGraphicsState
110      ###初期化
111      refMe's NSGraphicsContext's saveGraphicsState()
112      set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
113      (ocidContext's setShouldAntialias:(true))
114      (ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh))
115      (ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric))
116      ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0))
117      (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
118      #ペースト処理
119      repeat with itemAliasFilePath in listImageFilePath
120         #ファイル
121         set aliasFilePath to itemAliasFilePath as alias
122         set strFilePath to (POSIX path of aliasFilePath) as text
123         set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
124         set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
125         set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
126         #NSDATA
127         set ocidOption to (refMe's NSDataReadingMappedIfSafe)
128         set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
129         set ocidReadData to (first item of listResponse)
130         #NSIMAGE
131         set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
132         #NSIMAGEREP
133         set ocidImageRepArray to ocidReadImage's representations()
134         set ocidImageRep to ocidImageRepArray's firstObject()
135         #ピクセルサイズ
136         set numPxW to ocidImageRep's pixelsWide()
137         set numPxH to ocidImageRep's pixelsHigh()
138         set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH)
139         (ocidImageRep's setSize:(ocidImageSizePx))
140         set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH)
141         #ペースト位置
142         if numSetWpx = (missing value) then
143            set numSetWpx to 0 as integer
144         end if
145         set numSetHpx to (numMaxHpx - numPxH) as integer
146         set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH))
147         #元画像をペースト
148         set ocidOption to (refMe's NSCompositingOperationSourceOver)
149         (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value))
150         #次の画像のペースト位置
151         set numSetWpx to (numSetWpx + numPxW) as integer
152         #データは一旦解放
153         set ocidImageRep to (missing value)
154         set ocidReadImage to (missing value)
155         set ocidReadData to (missing value)
156      end repeat
157      
158      ####画像作成終了
159      refMe's NSGraphicsContext's restoreGraphicsState()
160      #▲▲▲ restoreGraphicsState
161      
162      
163      
164      
165   else if numDoMakeImage = 1 then
166      #保存先
167      set strSaveFileName to ("" & strDateNo & "-" & numTotalWpx & "x" & numMaxHpx & ".png") as text
168      set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
169      #処理開始
170      set intBPR to (4 * numTotalWpx) as integer
171      set 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)
172      #合成
173      set numSetWpx to (missing value)
174      #▼▼▼saveGraphicsState
175      ###初期化
176      refMe's NSGraphicsContext's saveGraphicsState()
177      set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
178      (ocidContext's setShouldAntialias:(true))
179      (ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh))
180      (ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric))
181      ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0))
182      (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
183      #ペースト処理
184      repeat with itemAliasFilePath in listImageFilePath
185         #ファイル
186         set aliasFilePath to itemAliasFilePath as alias
187         set strFilePath to (POSIX path of aliasFilePath) as text
188         set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
189         set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
190         set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
191         #NSDATA
192         set ocidOption to (refMe's NSDataReadingMappedIfSafe)
193         set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
194         set ocidReadData to (first item of listResponse)
195         #NSIMAGE
196         set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
197         #NSIMAGEREP
198         set ocidImageRepArray to ocidReadImage's representations()
199         set ocidImageRep to ocidImageRepArray's firstObject()
200         #ピクセルサイズ
201         set numPxW to ocidImageRep's pixelsWide()
202         set numPxH to ocidImageRep's pixelsHigh()
203         set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH)
204         (ocidImageRep's setSize:(ocidImageSizePx))
205         set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH)
206         #ペースト位置
207         #ペースト位置
208         if numSetWpx = (missing value) then
209            set numSetWpx to (numTotalWpx - numPxW) as integer
210         else
211            set numSetWpx to (numTotalWpx - numPxW) as integer
212         end if
213         set numTotalWpx to numSetWpx as integer
214         set numSetHpx to (numMaxHpx - numPxH) as integer
215         set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH))
216         #元画像をペースト
217         set ocidOption to (refMe's NSCompositingOperationSourceOver)
218         (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value))
219         #次の画像のペースト位置
220         set numSetWpx to (numSetWpx - numPxW) as integer
221         #データは一旦解放
222         set ocidImageRep to (missing value)
223         set ocidReadImage to (missing value)
224         set ocidReadData to (missing value)
225      end repeat
226      
227      ####画像作成終了
228      refMe's NSGraphicsContext's restoreGraphicsState()
229      #▲▲▲ restoreGraphicsState
230      
231      
232   else if numDoMakeImage = 2 then
233      #保存先
234      set strSaveFileName to ("" & strDateNo & "-" & numMaxWpx & "x" & numTotalHpx & ".png") as text
235      set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
236      #画像作成開始
237      set intBPR to (4 * numMaxWpx) as integer
238      set 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)
239      #合成
240      set numSetHpx to (missing value)
241      #▼▼▼saveGraphicsState
242      ###初期化
243      refMe's NSGraphicsContext's saveGraphicsState()
244      set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
245      (ocidContext's setShouldAntialias:(true))
246      (ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh))
247      (ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric))
248      ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0))
249      (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
250      #ペースト処理
251      repeat with itemAliasFilePath in listImageFilePath
252         #ファイル
253         set aliasFilePath to itemAliasFilePath as alias
254         set strFilePath to (POSIX path of aliasFilePath) as text
255         set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
256         set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
257         set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
258         #NSDATA
259         set ocidOption to (refMe's NSDataReadingMappedIfSafe)
260         set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
261         set ocidReadData to (first item of listResponse)
262         #NSIMAGE
263         set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
264         #NSIMAGEREP
265         set ocidImageRepArray to ocidReadImage's representations()
266         set ocidImageRep to ocidImageRepArray's firstObject()
267         #ピクセルサイズ
268         set numPxW to ocidImageRep's pixelsWide()
269         set numPxH to ocidImageRep's pixelsHigh()
270         set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH)
271         (ocidImageRep's setSize:(ocidImageSizePx))
272         set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH)
273         #ペースト位置
274         set numSetHpx to (numTotalHpx - numPxH) as integer
275         set numSetWpx to (numMaxWpx - numPxW) as integer
276         set ocidDrawRect to refMe's NSRect's NSMakeRect(0, numSetHpx, (numPxW), (numPxH))
277         #   set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH))
278         #元画像をペースト
279         set ocidOption to (refMe's NSCompositingOperationSourceOver)
280         (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value))
281         #次の画像のペースト位置
282         set numTotalHpx to (numSetHpx) as integer
283         #データは一旦解放
284         set ocidImageRep to (missing value)
285         set ocidReadImage to (missing value)
286         set ocidReadData to (missing value)
287      end repeat
288      
289      ####画像作成終了
290      refMe's NSGraphicsContext's restoreGraphicsState()
291      #▲▲▲ restoreGraphicsState
292      
293   end if
294   
295   
296   #PNG変換
297   set ocidProperty to refMe's NSMutableDictionary's alloc()'s init()
298   ocidProperty's setObject:(false) forKey:(refMe's NSImageInterlaced)
299   ocidProperty's setObject:(2.2) forKey:(refMe's NSImageGamma)
300   set ocidType to (refMe's NSBitmapImageFileTypePNG)
301   set ocidSaveData to (ocidAardboardRep's representationUsingType:(ocidType) |properties|:(ocidProperty))
302   
303   #保存
304   set ocidOption to (refMe's NSDataWritingAtomic)
305   set listDone to ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)
306   
307   #保存先を開く
308   set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
309   set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
310   
311   
312   return boolDone
313end doMakeImage
314
315
316########################
317# 画像サイズ取得
318to doGetImageSize(listImageFilePath)
319   set numMaxWpx to 0 as integer
320   set numMaxHpx to 0 as integer
321   set numTotalWpx to 0 as integer
322   set numTotalHpx to 0 as integer
323   #
324   repeat with itemImageFilePath in listImageFilePath
325      set aliasFilePath to itemImageFilePath as alias
326      set strFilePath to (POSIX path of aliasFilePath) as text
327      set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
328      set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
329      set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
330      #ピクセルサイズ
331      set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
332      set numWpx to ocidImageRep's pixelsWide()
333      set numHpx to ocidImageRep's pixelsHigh()
334      
335      #最大ピクセルサイズ取得
336      if numMaxWpx < numWpx then
337         set numMaxWpx to numWpx as integer
338      end if
339      if numMaxHpx < numHpx then
340         set numMaxHpx to numHpx as integer
341      end if
342      #合計ピクセルサイズ取得
343      set numTotalWpx to (numTotalWpx + numWpx) as integer
344      set numTotalHpx to (numTotalHpx + numHpx) as integer
345   end repeat
346   set listResponse to {numMaxWpx, numMaxHpx, numTotalWpx, numTotalHpx} as list
347   return listResponse
348end doGetImageSize
349
350
351########################
352#保存先確保
353to doMakeSaveDir()
354   set appFileManager to refMe's NSFileManager's defaultManager()
355   #出力画像保存先
356   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask))
357   set ocidPicturesDirPathURL to ocidURLsArray's firstObject()
358   set ocidSaveDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:("Image_Output") isDirectory:(true)
359   set ocidSaveDirPath to ocidSaveDirPathURL's |path|()
360   set boolExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
361   if boolExists is false then
362      #フォルダ作っておく
363      set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
364      ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
365      set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
366   end if
367   return ocidSaveDirPathURL
368end doMakeSaveDir
369
370
371########################
372#ダイアログ
373to doChooseFile()
374   set strMsg to ("画像ファイルを 選択") as text
375   set strPrompt to ("画像ファイルを を選択してください" & return & "複数選択可") as text
376   set aliasDesktopDirPath to (path to desktop from user domain) as alias
377   set listUTI to {"public.image"} as list
378   try
379      tell application "SystemUIServer"
380         activate
381         set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
382      end tell
383   on error strErrMes number numErrNo
384      log strErrMes & numErrNo
385      return false
386   end try
387   if listAliasFilePath is {} then
388      return false
389   end if
390   return listAliasFilePath
391end doChooseFile
392
393#############################
394#ダイアログ
395on doChooseList()
396   
397   set listJobs to {"【左→右】横方向(L2R)", "【右→左】横方向(R2L)", "【画像合成】縦方向"} as list
398   set strTitle to ("選んでください") as text
399   set strPrompt to ("ひとつ選んでください") as text
400   set strOK to ("OK") as text
401   set strCancel to ("キャンセル") as text
402   try
403      tell application "System Events"
404         activate
405         set valueResponse to (choose from list listJobs with title strTitle with prompt strPrompt default items (last item of listJobs) OK button name strOK cancel button name strCancel with empty selection allowed without multiple selections allowed)
406      end tell
407   on error strErrMes number numErrNo
408      log strErrMes & numErrNo
409      return false
410   end try
411   if (class of valueResponse) is boolean then
412      error "ユーザによってキャンセルされました。" number -128
413   else if (class of valueResponse) is list then
414      if valueResponse is {} then
415         error "Error 何も選んでいません" number -128
416      else
417         set strResponse to (first item of valueResponse) as text
418      end if
419   end if
420   #########
421   
422   if strResponse is "【左→右】横方向(L2R)" then
423      return 0
424   else if strResponse is "【右→左】横方向(R2L)" then
425      return 1
426   else if strResponse is "【画像合成】縦方向" then
427      return 2
428   else
429      return false
430   end if
431   
432end doChooseList
433
434
435#############################
436#日付情報の取得
437to doGetDateNo(argFormatStrings)
438   set ocidDate to refMe's NSDate's |date|()
439   set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
440   ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
441   ocidNSDateFormatter's setDateFormat:(argFormatStrings)
442   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
443   set strDateAndTime to ocidDateAndTime as text
444   return strDateAndTime
445end doGetDateNo
AppleScriptで生成しました