From a1b7497f0cc3d2c7efe1cc75e58a961192e4e2c8 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:38:09 +0200 Subject: [PATCH] improve crop --- internal/converter/converter.go | 1 + .../converter/options/converter_options.go | 4 +++ internal/epub/image/epub_image_options.go | 1 + .../imageprocessing/epub_image_processing.go | 2 +- .../epub_image_processing_helpers.go | 28 +++++++++++++++---- main.go | 1 + 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index cfadda1..bcd5e6a 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -98,6 +98,7 @@ func (c *Converter) InitParse() { c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.AvailableProfiles())) c.AddIntParam(&c.Options.Quality, "quality", c.Options.Quality, "Quality of the image") c.AddBoolParam(&c.Options.Crop, "crop", c.Options.Crop, "Crop images") + c.AddIntParam(&c.Options.CropRatio, "crop-ratio", c.Options.CropRatio, "Crop ratio: ratio of pixels allow to be non blank while cutting. 0 mean first non blank stop cutting. 5 may help to remove number.") c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker") c.AddIntParam(&c.Options.Contrast, "contrast", c.Options.Contrast, "Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast") c.AddBoolParam(&c.Options.AutoRotate, "autorotate", c.Options.AutoRotate, "Auto Rotate page when width > height") diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index c16cb90..cbfee49 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -24,6 +24,7 @@ type Options struct { Profile string `yaml:"profile"` Quality int `yaml:"quality"` Crop bool `yaml:"crop"` + CropRatio int `yaml:"crop_ratio"` Brightness int `yaml:"brightness"` Contrast int `yaml:"contrast"` Auto bool `yaml:"-"` @@ -59,6 +60,7 @@ func New() *Options { Profile: "", Quality: 85, Crop: true, + CropRatio: 5, Brightness: 0, Contrast: 0, AutoRotate: false, @@ -159,6 +161,7 @@ func (o *Options) ShowConfig() string { View : %s Quality : %d Crop : %v + CropRatio : %d Brightness : %d Contrast : %d AutoRotate : %v @@ -174,6 +177,7 @@ func (o *Options) ShowConfig() string { viewDesc, o.Quality, o.Crop, + o.CropRatio, o.Brightness, o.Contrast, o.AutoRotate, diff --git a/internal/epub/image/epub_image_options.go b/internal/epub/image/epub_image_options.go index 3797036..03dda90 100644 --- a/internal/epub/image/epub_image_options.go +++ b/internal/epub/image/epub_image_options.go @@ -3,6 +3,7 @@ package epubimage // options for image transformation type Options struct { Crop bool + CropRatio int ViewWidth int ViewHeight int Quality int diff --git a/internal/epub/imageprocessing/epub_image_processing.go b/internal/epub/imageprocessing/epub_image_processing.go index fdafe59..aaa6ac7 100644 --- a/internal/epub/imageprocessing/epub_image_processing.go +++ b/internal/epub/imageprocessing/epub_image_processing.go @@ -102,7 +102,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) { } if o.Image.Crop { - g := gift.New(gift.Crop(findMarging(src))) + g := gift.New(gift.Crop(findMarging(src, o.Image.CropRatio))) newSrc := image.NewNRGBA(g.Bounds(src.Bounds())) g.Draw(newSrc, src) src = newSrc diff --git a/internal/epub/imageprocessing/epub_image_processing_helpers.go b/internal/epub/imageprocessing/epub_image_processing_helpers.go index 067da84..e4f204d 100644 --- a/internal/epub/imageprocessing/epub_image_processing_helpers.go +++ b/internal/epub/imageprocessing/epub_image_processing_helpers.go @@ -21,18 +21,22 @@ func isSupportedImage(path string) bool { // check if the color is blank enough func colorIsBlank(c color.Color) bool { g := color.GrayModel.Convert(c).(color.Gray) - return g.Y >= 0xf0 + return g.Y >= 0xe0 } // lookup for margin (blank) around the image -func findMarging(img image.Image) image.Rectangle { +func findMarging(img image.Image, cutRatio int) image.Rectangle { imgArea := img.Bounds() LEFT: for x := imgArea.Min.X; x < imgArea.Max.X; x++ { + allowNonBlank := imgArea.Dy() * cutRatio / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { - break LEFT + allowNonBlank-- + if allowNonBlank <= 0 { + break LEFT + } } } imgArea.Min.X++ @@ -40,9 +44,13 @@ LEFT: UP: for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { + allowNonBlank := imgArea.Dx() * cutRatio / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { - break UP + allowNonBlank-- + if allowNonBlank <= 0 { + break UP + } } } imgArea.Min.Y++ @@ -50,9 +58,13 @@ UP: RIGHT: for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- { + allowNonBlank := imgArea.Dy() * cutRatio / 100 for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { if !colorIsBlank(img.At(x, y)) { - break RIGHT + allowNonBlank-- + if allowNonBlank <= 0 { + break RIGHT + } } } imgArea.Max.X-- @@ -60,9 +72,13 @@ RIGHT: BOTTOM: for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- { + allowNonBlank := imgArea.Dx() * cutRatio / 100 for x := imgArea.Min.X; x < imgArea.Max.X; x++ { if !colorIsBlank(img.At(x, y)) { - break BOTTOM + allowNonBlank-- + if allowNonBlank <= 0 { + break BOTTOM + } } } imgArea.Max.Y-- diff --git a/main.go b/main.go index a0f937b..783a4a5 100644 --- a/main.go +++ b/main.go @@ -114,6 +114,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s ViewHeight: perfectHeight, Quality: cmd.Options.Quality, Crop: cmd.Options.Crop, + CropRatio: cmd.Options.CropRatio, Brightness: cmd.Options.Brightness, Contrast: cmd.Options.Contrast, AutoRotate: cmd.Options.AutoRotate,