improve crop

This commit is contained in:
Celogeek 2023-04-26 17:38:09 +02:00
parent 0637d64b93
commit a1b7497f0c
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 30 additions and 7 deletions

View File

@ -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")

View File

@ -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,

View File

@ -3,6 +3,7 @@ package epubimage
// options for image transformation
type Options struct {
Crop bool
CropRatio int
ViewWidth int
ViewHeight int
Quality int

View File

@ -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

View File

@ -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--

View File

@ -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,