mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
add skip crop if limit reached
This commit is contained in:
parent
c500755a4e
commit
0dde6e02a4
@ -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.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.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.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")
|
||||
|
@ -31,6 +31,7 @@ type Options struct {
|
||||
CropRatioRight int `yaml:"crop_ratio_right"`
|
||||
CropRatioBottom int `yaml:"crop_ratio_bottom"`
|
||||
CropLimit int `yaml:"crop_limit"`
|
||||
CropSkipIfLimitReached bool `yaml:"crop_skip_if_limit_reached"`
|
||||
Brightness int `yaml:"brightness"`
|
||||
Contrast int `yaml:"contrast"`
|
||||
AutoContrast bool `yaml:"auto_contrast"`
|
||||
@ -89,6 +90,8 @@ func New() *Options {
|
||||
CropRatioUp: 1,
|
||||
CropRatioRight: 1,
|
||||
CropRatioBottom: 3,
|
||||
CropLimit: 10,
|
||||
CropSkipIfLimitReached: true,
|
||||
NoBlankImage: true,
|
||||
HasCover: true,
|
||||
KeepDoublePageIfSplit: true,
|
||||
@ -164,6 +167,7 @@ func (o *Options) MarshalJSON() ([]byte, error) {
|
||||
"bottom": o.CropRatioBottom,
|
||||
}
|
||||
out["crop_limit"] = o.CropLimit
|
||||
out["crop_skip_if_limit_reached"] = o.CropSkipIfLimitReached
|
||||
}
|
||||
if o.Brightness != 0 {
|
||||
out["brightness"] = o.Brightness
|
||||
@ -268,7 +272,7 @@ func (o *Options) ShowConfig() string {
|
||||
{"Grayscale", o.Grayscale, true},
|
||||
{"Grayscale mode", grayscaleMode, o.Grayscale},
|
||||
{"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},
|
||||
{"Contrast", o.Contrast, o.Contrast != 0},
|
||||
{"Auto contrast", o.AutoContrast, true},
|
||||
|
@ -8,9 +8,9 @@ import (
|
||||
)
|
||||
|
||||
// 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(
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
maxCutX, maxCutY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100
|
||||
maxCropX, maxCropY := imgArea.Dx()*limit/100, imgArea.Dy()*limit/100
|
||||
|
||||
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
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
@ -42,10 +42,13 @@ LEFT:
|
||||
}
|
||||
}
|
||||
imgArea.Min.X++
|
||||
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
|
||||
return bounds
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
@ -56,10 +59,13 @@ UP:
|
||||
}
|
||||
}
|
||||
imgArea.Min.Y++
|
||||
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
|
||||
return bounds
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
@ -70,10 +76,13 @@ RIGHT:
|
||||
}
|
||||
}
|
||||
imgArea.Max.X--
|
||||
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
|
||||
return bounds
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
@ -84,6 +93,9 @@ BOTTOM:
|
||||
}
|
||||
}
|
||||
imgArea.Max.Y--
|
||||
if limit > 0 && maxCrop == 1 && skipIfLimitReached {
|
||||
return bounds
|
||||
}
|
||||
}
|
||||
|
||||
return imgArea
|
||||
|
@ -191,6 +191,7 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *
|
||||
e.Image.Crop.Right,
|
||||
e.Image.Crop.Bottom,
|
||||
e.Image.Crop.Limit,
|
||||
e.Image.Crop.SkipIfLimitReached,
|
||||
)
|
||||
|
||||
// detect if blank image
|
||||
|
@ -7,6 +7,7 @@ type Crop struct {
|
||||
Enabled bool
|
||||
Left, Up, Right, Bottom int
|
||||
Limit int
|
||||
SkipIfLimitReached bool
|
||||
}
|
||||
|
||||
type Color struct {
|
||||
|
1
main.go
1
main.go
@ -130,6 +130,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
Right: cmd.Options.CropRatioRight,
|
||||
Bottom: cmd.Options.CropRatioBottom,
|
||||
Limit: cmd.Options.CropLimit,
|
||||
SkipIfLimitReached: cmd.Options.CropSkipIfLimitReached,
|
||||
},
|
||||
Quality: cmd.Options.Quality,
|
||||
Brightness: cmd.Options.Brightness,
|
||||
|
Loading…
x
Reference in New Issue
Block a user