▲Original Image
▲Filtered Image(CIColorMonochrome)
▲Filtered Image(CIColorPosterize)
▲Result Image
AppleScript名:画像を文字認識して文字エリアを塗りつぶし v3 |
— Created 2017-11-19 by Takaaki Naganoya — Modified 2018-02-11 by Takaaki Naganoya –v3:画像の前処理を付加 use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" use framework "QuartzCore" use framework "AppKit" set retinaF to (current application’s NSScreen’s mainScreen()’s backingScaleFactor()) as real –> 2.0 (Retina) / 1.0 (Non Retina) set imgPath to POSIX path of (choose file of type {"public.image"}) set anImage to current application’s NSImage’s alloc()’s initWithContentsOfFile:imgPath set aCIImage to convNSImageToCIimage(anImage) –モノクロ化フィルタ set bCIImage to monochromefilterACGImage(aCIImage) of me –2階調ポスタライズフィルタ set cCIImage to posterizefilterACGImage(bCIImage) of me –文字領域認識 set detectList to textDetect(cCIImage) of me –描画開始 anImage’s lockFocus() repeat with i in detectList set origX to (x of origin of i) / retinaF set origY to (y of origin of i) / retinaF set sizeX to (width of |size| of i) / retinaF set sizeY to (height of |size| of i) / retinaF set theRect to {{x:origX, y:origY}, {width:sizeX, height:sizeY}} set theNSBezierPath to current application’s NSBezierPath’s bezierPath (theNSBezierPath’s appendBezierPathWithRect:theRect) set rRnd to (random number from 1 to 10) / 10 set gRnd to (random number from 1 to 10) / 10 set bRnd to (random number from 1 to 10) / 10 set fillColor to (current application’s NSColor’s colorWithCalibratedRed:rRnd green:gRnd blue:bRnd alpha:0.6) fillColor’s |set|() –色設定 theNSBezierPath’s fill() –ぬりつぶし end repeat anImage’s unlockFocus() –描画ここまで set aUUIDstr to (current application’s NSUUID’s UUID()’s UUIDString()) as string set aPath to ((current application’s NSString’s stringWithString:imgPath)’s stringByDeletingLastPathComponent()’s stringByAppendingPathComponent:aUUIDstr)’s stringByAppendingPathExtension:"png" set fRes to saveImageRepAtPathAsPNG(anImage, aPath) of me on openImageFile(imageFile) — imageFile: POSIX path 形式のファイルパス set fileURL to current application’s |NSURL|’s fileURLWithPath:imageFile return current application’s CIImage’s alloc()’s initWithContentsOfURL:fileURL end openImageFile –画像を指定パスにPNG形式で保存 on saveImageRepAtPathAsPNG(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)) return (myNewImageData’s writeToFile:newPath atomically:true) as boolean end saveImageRepAtPathAsPNG on textDetect(imageRef) — 検出器のオプションを NSDictonary で作成 set optDic1 to current application’s NSDictionary’s dictionaryWithObject:(current application’s CIDetectorAccuracyHigh) forKey:(current application’s CIDetectorAccuracy) set textDetector to current application’s CIDetector’s detectorOfType:(current application’s CIDetectorTypeText) context:(missing value) options:optDic1 — 文字エリア検出を実行 set optDic2 to current application’s NSDictionary’s dictionaryWithObject:true forKey:(current application’s CIDetectorReturnSubFeatures) set textArray to textDetector’s featuresInImage:imageRef options:optDic2 set fList to {} — 検出されたテキストの位置とサイズをログに出力 repeat with i from 1 to (count of textArray) set typeFace to item i of textArray set bList to (typeFace’s subFeatures()) repeat with ii in bList set aBounds to ii’s |bounds|() set aType to ii’s type() set the end of fList to aBounds end repeat end repeat return fList end textDetect on convCIimageToNSImage(aCIImage) set aRep to current application’s NSBitmapImageRep’s alloc()’s initWithCIImage:aCIImage set tmpSize to aRep’s |size|() set newImg to current application’s NSImage’s alloc()’s initWithSize:tmpSize newImg’s addRepresentation:aRep return newImg end convCIimageToNSImage on convNSImageToCIimage(aNSImage) set tiffDat to aNSImage’s TIFFRepresentation() set aRep to current application’s NSBitmapImageRep’s imageRepWithData:tiffDat set newImg to current application’s CIImage’s alloc()’s initWithBitmapImageRep:aRep return newImg end convNSImageToCIimage –Posterizeフィルタ on posterizefilterACGImage(aCIImage) set aFilter to current application’s CIFilter’s filterWithName:"CIColorPosterize" aFilter’s setDefaults() aFilter’s setValue:aCIImage forKey:"inputImage" aFilter’s setValue:2 forKey:"inputLevels" set aOutImage to aFilter’s valueForKey:"outputImage" return aOutImage end posterizefilterACGImage –Monochromeフィルタ on monochromefilterACGImage(aCIImage) set aFilter to current application’s CIFilter’s filterWithName:"CIColorMonochrome" aFilter’s setDefaults() aFilter’s setValue:aCIImage forKey:"inputImage" aFilter’s setValue:1.0 forKey:"inputIntensity" set aOutImage to aFilter’s valueForKey:"outputImage" return aOutImage end monochromefilterACGImage |
More from my site
(Visited 145 times, 1 visits today)