ターゲット画像に対して配列に入れた複数の画像を類似度をキーにしてソートするAppleScriptです。
最も類似度が高いと思われる画像をデスクトップにPNG形式で書き出します。
CocoaImageHashing.framework (To ~/Library/Frameworks/)
AppleScript名:配列に入れた画像を類似度でソートする |
— Created 2016-10-30 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "CocoaImageHashing" –https://github.com/ameingast/cocoaimagehashing –From Example: "Sorting an NSArray containing image data" –比較元の画像を選択 set baseData to retDataFromPath(POSIX path of (choose file {"public.image"})) of me –比較対象のデータを選択 set aData to retDataFromPath(POSIX path of (choose file {"public.image"})) of me set bData to retDataFromPath(POSIX path of (choose file {"public.image"})) of me set cData to retDataFromPath(POSIX path of (choose file {"public.image"})) of me set aList to {aData, bData, cData} set anArray to current application’s NSMutableArray’s arrayWithArray:aList –配列に入れられた画像を類似度でソートする set aRes to (current application’s OSImageHashing’s sharedInstance()’s sortedArrayUsingImageSimilartyComparator:baseData forArray:anArray) –最も類似度の高い画像データを取り出す set firstObj to aRes’s objectAtIndex:0 set anImage to current application’s NSImage’s alloc()’s initWithData:firstObj –確認のため、デスクトップにPNG形式で最も類似度の高い画像を書き出す set aFolder to POSIX path of (path to desktop folder) set fRes to retUUIDfilePathFromFolder(aFolder, "png") of me set sRes to saveNSImageAtPathAsPNG(anImage, fRes) of me on retUUIDfilePathFromFolder(aFolder, aEXT) set aUUIDstr to (current application’s NSUUID’s UUID()’s UUIDString()) as string set aPath to ((current application’s NSString’s stringWithString:aFolder)’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:aEXT return aPath end retUUIDfilePathFromFolder –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to current application’s NSBitmapImageRep’s imageRepWithData:imageRep set pathString to current application’s NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –true/false end saveNSImageAtPathAsPNG on retDataFromPath(aFile) set aURL to current application’s |NSURL|’s fileURLWithPath:aFile set aData to current application’s NSData’s dataWithContentsOfURL:aURL return aData end retDataFromPath |
More from my site
(Visited 192 times, 2 visits today)
ぴよまるソフトウェアが選ぶ、2018年に書いた「価値あるScript」 – AppleScriptの穴 says:
[…] ・配列に入れた画像を類似度でソートする こういう処理はぜひAppleScriptでクリアしておきたい内容です。Cocoaの機能に手を出しはじめたのも、REST APIの呼び出しや機械学習(強化学習)や […]