指定の画像ファイルをグレースケールのPNG画像ファイルに変換するAppleScriptです。
Appleが配布しているMNISTの手書き数字画像の認識CoreMLモデルをCocoa Framework化してみたところ、グレースケールの画像を要求されたため、画像をグレースケール化するルーチンを作ってみました。
結局、MNISTの手書き画像認識はうまく行かなかったのですが(全部「0」が返ってくる、、、)、グレースケール変換ルーチンは実用的だったので載せておくことにしました。
AppleScript名:指定画像をPNG形式(グレースケール)で保存 |
— Created 2019-07-09 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSUUID : a reference to current application’s NSUUID property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSColorSpace : a reference to current application’s NSColorSpace property NSPNGFileType : a reference to current application’s NSPNGFileType property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSColorRenderingIntentPerceptual : a reference to current application’s NSColorRenderingIntentPerceptual set aFile to POSIX path of (choose file of type {"public.image"} with prompt "Select Image A") set aImage to NSImage’s alloc()’s initWithContentsOfFile:aFile set fRes to retUUIDfilePath(aFile, "png") of me set sRes to saveNSImageAtPathAsGreyPNG(aImage, fRes) of me –指定ファイルパスを元にUUIDのファイル名、指定拡張子をつけたファイルパスを作成して返す on retUUIDfilePath(aPath, aEXT) set aUUIDstr to (NSUUID’s UUID()’s UUIDString()) as string set aPath to ((NSString’s stringWithString:aPath)’s stringByDeletingLastPathComponent()’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:aEXT return aPath end retUUIDfilePath –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsGreyPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep –ビットマップをグレースケール変換 set bRawimg to convBitMapToDeviceGrey(aRawimg) of me set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (bRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –true/false end saveNSImageAtPathAsGreyPNG –NSBitmapImageRepをグレースケールに変換する on convBitMapToDeviceGrey(aBitmap) set aSpace to NSColorSpace’s deviceGrayColorSpace() set bRawimg to aBitmap’s bitmapImageRepByConvertingToColorSpace:aSpace renderingIntent:(NSColorRenderingIntentPerceptual) return bRawimg end convBitMapToDeviceGrey |
AppleScript名:指定画像をグレースケールのNSImageに変換 |
— Created 2019-07-09 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSUUID : a reference to current application’s NSUUID property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSColorSpace : a reference to current application’s NSColorSpace property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSColorRenderingIntentDefault : a reference to current application’s NSColorRenderingIntentDefault set aFile to POSIX path of (choose file of type {"public.image"} with prompt "Select Image") set aImage to NSImage’s alloc()’s initWithContentsOfFile:aFile set nsImageRes to convNSImageAsGrey(aImage) of me –NSImageをグレースケールに変換する(NSImageで入出力) on convNSImageAsGrey(anImage) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set aSpace to NSColorSpace’s deviceGrayColorSpace() set bRawimg to aRawimg’s bitmapImageRepByConvertingToColorSpace:aSpace renderingIntent:(NSColorRenderingIntentDefault) set outImage to NSImage’s alloc()’s initWithSize:(bRawimg’s |size|()) outImage’s addRepresentation:(bRawimg) return outImage end convNSImageAsGrey |
More from my site
(Visited 167 times, 1 visits today)