add skip crop if limit reached

This commit is contained in:
Celogeek 2024-04-28 19:28:35 +02:00
parent c500755a4e
commit 0dde6e02a4
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 52 additions and 32 deletions

View File

@ -114,6 +114,7 @@ func (c *Converter) InitParse() {
c.AddIntParam(&c.Options.CropRatioRight, "crop-ratio-right", c.Options.CropRatioRight, "Crop ratio right: ratio of pixels allow to be non blank while cutting on the right.") c.AddIntParam(&c.Options.CropRatioRight, "crop-ratio-right", c.Options.CropRatioRight, "Crop ratio right: ratio of pixels allow to be non blank while cutting on the right.")
c.AddIntParam(&c.Options.CropRatioBottom, "crop-ratio-bottom", c.Options.CropRatioBottom, "Crop ratio bottom: ratio of pixels allow to be non blank while cutting on the bottom.") c.AddIntParam(&c.Options.CropRatioBottom, "crop-ratio-bottom", c.Options.CropRatioBottom, "Crop ratio bottom: ratio of pixels allow to be non blank while cutting on the bottom.")
c.AddIntParam(&c.Options.CropLimit, "crop-limit", c.Options.CropLimit, "Crop limit: maximum number of cropping in percentage allowed. 0 mean unlimited.") c.AddIntParam(&c.Options.CropLimit, "crop-limit", c.Options.CropLimit, "Crop limit: maximum number of cropping in percentage allowed. 0 mean unlimited.")
c.AddBoolParam(&c.Options.CropSkipIfLimitReached, "crop-skip-if-limit-reached", c.Options.CropSkipIfLimitReached, "Crop skip if limit reached.")
c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustment: between -100 and 100, > 0 lighter, < 0 darker") c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustment: between -100 and 100, > 0 lighter, < 0 darker")
c.AddIntParam(&c.Options.Contrast, "contrast", c.Options.Contrast, "Contrast readjustment: between -100 and 100, > 0 more contrast, < 0 less contrast") c.AddIntParam(&c.Options.Contrast, "contrast", c.Options.Contrast, "Contrast readjustment: between -100 and 100, > 0 more contrast, < 0 less contrast")
c.AddBoolParam(&c.Options.AutoContrast, "autocontrast", c.Options.AutoContrast, "Improve contrast automatically") c.AddBoolParam(&c.Options.AutoContrast, "autocontrast", c.Options.AutoContrast, "Improve contrast automatically")

View File

@ -31,6 +31,7 @@ type Options struct {
CropRatioRight int `yaml:"crop_ratio_right"` CropRatioRight int `yaml:"crop_ratio_right"`
CropRatioBottom int `yaml:"crop_ratio_bottom"` CropRatioBottom int `yaml:"crop_ratio_bottom"`
CropLimit int `yaml:"crop_limit"` CropLimit int `yaml:"crop_limit"`
CropSkipIfLimitReached bool `yaml:"crop_skip_if_limit_reached"`
Brightness int `yaml:"brightness"` Brightness int `yaml:"brightness"`
Contrast int `yaml:"contrast"` Contrast int `yaml:"contrast"`
AutoContrast bool `yaml:"auto_contrast"` AutoContrast bool `yaml:"auto_contrast"`
@ -81,23 +82,25 @@ type Options struct {
// New Initialize default options. // New Initialize default options.
func New() *Options { func New() *Options {
return &Options{ return &Options{
Profile: "SR", Profile: "SR",
Quality: 85, Quality: 85,
Grayscale: true, Grayscale: true,
Crop: true, Crop: true,
CropRatioLeft: 1, CropRatioLeft: 1,
CropRatioUp: 1, CropRatioUp: 1,
CropRatioRight: 1, CropRatioRight: 1,
CropRatioBottom: 3, CropRatioBottom: 3,
NoBlankImage: true, CropLimit: 10,
HasCover: true, CropSkipIfLimitReached: true,
KeepDoublePageIfSplit: true, NoBlankImage: true,
SortPathMode: 1, HasCover: true,
ForegroundColor: "000", KeepDoublePageIfSplit: true,
BackgroundColor: "FFF", SortPathMode: 1,
Format: "jpeg", ForegroundColor: "000",
TitlePage: 1, BackgroundColor: "FFF",
profiles: profiles.New(), Format: "jpeg",
TitlePage: 1,
profiles: profiles.New(),
} }
} }
@ -164,6 +167,7 @@ func (o *Options) MarshalJSON() ([]byte, error) {
"bottom": o.CropRatioBottom, "bottom": o.CropRatioBottom,
} }
out["crop_limit"] = o.CropLimit out["crop_limit"] = o.CropLimit
out["crop_skip_if_limit_reached"] = o.CropSkipIfLimitReached
} }
if o.Brightness != 0 { if o.Brightness != 0 {
out["brightness"] = o.Brightness out["brightness"] = o.Brightness
@ -268,7 +272,7 @@ func (o *Options) ShowConfig() string {
{"Grayscale", o.Grayscale, true}, {"Grayscale", o.Grayscale, true},
{"Grayscale mode", grayscaleMode, o.Grayscale}, {"Grayscale mode", grayscaleMode, o.Grayscale},
{"Crop", o.Crop, true}, {"Crop", o.Crop, true},
{"Crop ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom - Limit %d%%", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit), o.Crop}, {"Crop ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom - Limit %d%% - Skip %v", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom, o.CropLimit, o.CropSkipIfLimitReached), o.Crop},
{"Brightness", o.Brightness, o.Brightness != 0}, {"Brightness", o.Brightness, o.Brightness != 0},
{"Contrast", o.Contrast, o.Contrast != 0}, {"Contrast", o.Contrast, o.Contrast != 0},
{"Auto contrast", o.AutoContrast, true}, {"Auto contrast", o.AutoContrast, true},

View File

@ -8,9 +8,9 @@ import (
) )
// AutoCrop Lookup for margin and crop // AutoCrop Lookup for margin and crop
func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int, limit int) gift.Filter { func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int, limit int, skipIfLimitReached bool) gift.Filter {
return gift.Crop( return gift.Crop(
findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit), findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}, limit, skipIfLimitReached),
) )
} }
@ -25,13 +25,13 @@ type cutRatioOptions struct {
Left, Up, Right, Bottom int Left, Up, Right, Bottom int
} }
func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int) image.Rectangle { func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions, limit int, skipIfLimitReached bool) image.Rectangle {
imgArea := bounds imgArea := bounds
maxCutX, maxCutY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100 maxCropX, maxCropY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100
LEFT: LEFT:
for x, maxCut := imgArea.Min.X, maxCutX; x < imgArea.Max.X && (maxCutX == 0 || maxCut > 0); x, maxCut = x+1, maxCut-1 { for x, maxCrop := imgArea.Min.X, maxCropX; x < imgArea.Max.X && (limit == 0 || maxCrop > 0); x, maxCrop = x+1, maxCrop-1 {
allowNonBlank := imgArea.Dy() * cutRatio.Left / 100 allowNonBlank := imgArea.Dy() * cutRatio.Left / 100
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) { if !colorIsBlank(img.At(x, y)) {
@ -42,10 +42,13 @@ LEFT:
} }
} }
imgArea.Min.X++ imgArea.Min.X++
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
} }
UP: UP:
for y, maxCut := imgArea.Min.Y, maxCutY; y < imgArea.Max.Y && (maxCutY == 0 || maxCut > 0); y, maxCut = y+1, maxCut-1 { for y, maxCrop := imgArea.Min.Y, maxCropY; y < imgArea.Max.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y+1, maxCrop-1 {
allowNonBlank := imgArea.Dx() * cutRatio.Up / 100 allowNonBlank := imgArea.Dx() * cutRatio.Up / 100
for x := imgArea.Min.X; x < imgArea.Max.X; x++ { for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) { if !colorIsBlank(img.At(x, y)) {
@ -56,10 +59,13 @@ UP:
} }
} }
imgArea.Min.Y++ imgArea.Min.Y++
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
} }
RIGHT: RIGHT:
for x, maxCut := imgArea.Max.X-1, maxCutX; x >= imgArea.Min.X && (maxCutX == 0 || maxCut > 0); x, maxCut = x-1, maxCut-1 { for x, maxCrop := imgArea.Max.X-1, maxCropX; x >= imgArea.Min.X && (limit == 0 || maxCrop > 0); x, maxCrop = x-1, maxCrop-1 {
allowNonBlank := imgArea.Dy() * cutRatio.Right / 100 allowNonBlank := imgArea.Dy() * cutRatio.Right / 100
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ { for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) { if !colorIsBlank(img.At(x, y)) {
@ -70,10 +76,13 @@ RIGHT:
} }
} }
imgArea.Max.X-- imgArea.Max.X--
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
} }
BOTTOM: BOTTOM:
for y, maxCut := imgArea.Max.Y-1, maxCutY; y >= imgArea.Min.Y && (maxCutY == 0 || maxCut > 0); y, maxCut = y-1, maxCut-1 { for y, maxCrop := imgArea.Max.Y-1, maxCropY; y >= imgArea.Min.Y && (limit == 0 || maxCrop > 0); y, maxCrop = y-1, maxCrop-1 {
allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100 allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100
for x := imgArea.Min.X; x < imgArea.Max.X; x++ { for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) { if !colorIsBlank(img.At(x, y)) {
@ -84,6 +93,9 @@ BOTTOM:
} }
} }
imgArea.Max.Y-- imgArea.Max.Y--
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
return bounds
}
} }
return imgArea return imgArea

View File

@ -191,6 +191,7 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *
e.Image.Crop.Right, e.Image.Crop.Right,
e.Image.Crop.Bottom, e.Image.Crop.Bottom,
e.Image.Crop.Limit, e.Image.Crop.Limit,
e.Image.Crop.SkipIfLimitReached,
) )
// detect if blank image // detect if blank image

View File

@ -7,6 +7,7 @@ type Crop struct {
Enabled bool Enabled bool
Left, Up, Right, Bottom int Left, Up, Right, Bottom int
Limit int Limit int
SkipIfLimitReached bool
} }
type Color struct { type Color struct {

13
main.go
View File

@ -124,12 +124,13 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Json: cmd.Options.Json, Json: cmd.Options.Json,
Image: &epuboptions.Image{ Image: &epuboptions.Image{
Crop: &epuboptions.Crop{ Crop: &epuboptions.Crop{
Enabled: cmd.Options.Crop, Enabled: cmd.Options.Crop,
Left: cmd.Options.CropRatioLeft, Left: cmd.Options.CropRatioLeft,
Up: cmd.Options.CropRatioUp, Up: cmd.Options.CropRatioUp,
Right: cmd.Options.CropRatioRight, Right: cmd.Options.CropRatioRight,
Bottom: cmd.Options.CropRatioBottom, Bottom: cmd.Options.CropRatioBottom,
Limit: cmd.Options.CropLimit, Limit: cmd.Options.CropLimit,
SkipIfLimitReached: cmd.Options.CropSkipIfLimitReached,
}, },
Quality: cmd.Options.Quality, Quality: cmd.Options.Quality,
Brightness: cmd.Options.Brightness, Brightness: cmd.Options.Brightness,