diff options
author | Nick White <git@njw.name> | 2019-10-02 12:32:59 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2019-10-02 12:32:59 +0100 |
commit | 94352c6124aed2d85cfb21e83c0dafb6f918fb7d (patch) | |
tree | 6bf574be2db265f714d16bf0140b5c3056b408e8 /preproc/wipesides.go | |
parent | 33e538c56f7513f1b98cab16de771a47a0e2a300 (diff) |
Add wipeonly queue and functionality
This is useful for prebinarised images, which don't need full preprocessing,
but do require wiping, albeit with a more conservative threshold.
Diffstat (limited to 'preproc/wipesides.go')
-rw-r--r-- | preproc/wipesides.go | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/preproc/wipesides.go b/preproc/wipesides.go index 24fb7bd..1b73791 100644 --- a/preproc/wipesides.go +++ b/preproc/wipesides.go @@ -4,8 +4,14 @@ package preproc // TODO: switch to an interface rather than integralimg.I import ( + "errors" + "fmt" "image" "image/color" + "image/draw" + _ "image/jpeg" + "image/png" + "os" "rescribe.xyz/go.git/integralimg" ) @@ -106,7 +112,7 @@ func toonarrow(img *image.Gray, lowedge int, highedge int, min int) bool { return false } -// wipe fills the sections of image which fall outside the content +// Wipe fills the sections of image which fall outside the content // area with white, providing the content area is above min % func Wipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray { integral := integralimg.ToIntegralImg(img) @@ -116,3 +122,39 @@ func Wipe(img *image.Gray, wsize int, thresh float64, min int) *image.Gray { } return wipesides(img, lowedge, highedge) } + +// WipeFile wipes an image file, filling the sections of the image +// which fall outside the content area with white, providing the +// content area is above min %. +// inPath: path of the input image. +// outPath: path to save the output image. +// wsize: window size for wipe algorithm. +// thresh: threshold for wipe algorithm. +// min: minimum % of content area width to consider valid. +func WipeFile(inPath string, outPath string, wsize int, thresh float64, min int) error { + f, err := os.Open(inPath) + defer f.Close() + if err != nil { + return errors.New(fmt.Sprintf("Could not open file %s: %v", inPath, err)) + } + img, _, err := image.Decode(f) + if err != nil { + return errors.New(fmt.Sprintf("Could not decode image: %v", err)) + } + b := img.Bounds() + gray := image.NewGray(image.Rect(0, 0, b.Dx(), b.Dy())) + draw.Draw(gray, b, img, b.Min, draw.Src) + + clean := Wipe(gray, wsize, thresh, min) + + f, err = os.Create(outPath) + if err != nil { + return errors.New(fmt.Sprintf("Could not create file %s: %v", outPath, err)) + } + defer f.Close() + err = png.Encode(f, clean) + if err != nil { + return errors.New(fmt.Sprintf("Could not encode image: %v", err)) + } + return nil +} |