mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 00:02:37 +02:00
improve crop
This commit is contained in:
parent
0637d64b93
commit
a1b7497f0c
@ -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.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.AddIntParam(&c.Options.Quality, "quality", c.Options.Quality, "Quality of the image")
|
||||||
c.AddBoolParam(&c.Options.Crop, "crop", c.Options.Crop, "Crop images")
|
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.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.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")
|
c.AddBoolParam(&c.Options.AutoRotate, "autorotate", c.Options.AutoRotate, "Auto Rotate page when width > height")
|
||||||
|
@ -24,6 +24,7 @@ type Options struct {
|
|||||||
Profile string `yaml:"profile"`
|
Profile string `yaml:"profile"`
|
||||||
Quality int `yaml:"quality"`
|
Quality int `yaml:"quality"`
|
||||||
Crop bool `yaml:"crop"`
|
Crop bool `yaml:"crop"`
|
||||||
|
CropRatio int `yaml:"crop_ratio"`
|
||||||
Brightness int `yaml:"brightness"`
|
Brightness int `yaml:"brightness"`
|
||||||
Contrast int `yaml:"contrast"`
|
Contrast int `yaml:"contrast"`
|
||||||
Auto bool `yaml:"-"`
|
Auto bool `yaml:"-"`
|
||||||
@ -59,6 +60,7 @@ func New() *Options {
|
|||||||
Profile: "",
|
Profile: "",
|
||||||
Quality: 85,
|
Quality: 85,
|
||||||
Crop: true,
|
Crop: true,
|
||||||
|
CropRatio: 5,
|
||||||
Brightness: 0,
|
Brightness: 0,
|
||||||
Contrast: 0,
|
Contrast: 0,
|
||||||
AutoRotate: false,
|
AutoRotate: false,
|
||||||
@ -159,6 +161,7 @@ func (o *Options) ShowConfig() string {
|
|||||||
View : %s
|
View : %s
|
||||||
Quality : %d
|
Quality : %d
|
||||||
Crop : %v
|
Crop : %v
|
||||||
|
CropRatio : %d
|
||||||
Brightness : %d
|
Brightness : %d
|
||||||
Contrast : %d
|
Contrast : %d
|
||||||
AutoRotate : %v
|
AutoRotate : %v
|
||||||
@ -174,6 +177,7 @@ func (o *Options) ShowConfig() string {
|
|||||||
viewDesc,
|
viewDesc,
|
||||||
o.Quality,
|
o.Quality,
|
||||||
o.Crop,
|
o.Crop,
|
||||||
|
o.CropRatio,
|
||||||
o.Brightness,
|
o.Brightness,
|
||||||
o.Contrast,
|
o.Contrast,
|
||||||
o.AutoRotate,
|
o.AutoRotate,
|
||||||
|
@ -3,6 +3,7 @@ package epubimage
|
|||||||
// options for image transformation
|
// options for image transformation
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Crop bool
|
Crop bool
|
||||||
|
CropRatio int
|
||||||
ViewWidth int
|
ViewWidth int
|
||||||
ViewHeight int
|
ViewHeight int
|
||||||
Quality int
|
Quality int
|
||||||
|
@ -102,7 +102,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if o.Image.Crop {
|
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()))
|
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
|
||||||
g.Draw(newSrc, src)
|
g.Draw(newSrc, src)
|
||||||
src = newSrc
|
src = newSrc
|
||||||
|
@ -21,18 +21,22 @@ func isSupportedImage(path string) bool {
|
|||||||
// check if the color is blank enough
|
// check if the color is blank enough
|
||||||
func colorIsBlank(c color.Color) bool {
|
func colorIsBlank(c color.Color) bool {
|
||||||
g := color.GrayModel.Convert(c).(color.Gray)
|
g := color.GrayModel.Convert(c).(color.Gray)
|
||||||
return g.Y >= 0xf0
|
return g.Y >= 0xe0
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookup for margin (blank) around the image
|
// 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()
|
imgArea := img.Bounds()
|
||||||
|
|
||||||
LEFT:
|
LEFT:
|
||||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
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++ {
|
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||||
if !colorIsBlank(img.At(x, y)) {
|
if !colorIsBlank(img.At(x, y)) {
|
||||||
break LEFT
|
allowNonBlank--
|
||||||
|
if allowNonBlank <= 0 {
|
||||||
|
break LEFT
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imgArea.Min.X++
|
imgArea.Min.X++
|
||||||
@ -40,9 +44,13 @@ LEFT:
|
|||||||
|
|
||||||
UP:
|
UP:
|
||||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
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++ {
|
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||||
if !colorIsBlank(img.At(x, y)) {
|
if !colorIsBlank(img.At(x, y)) {
|
||||||
break UP
|
allowNonBlank--
|
||||||
|
if allowNonBlank <= 0 {
|
||||||
|
break UP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imgArea.Min.Y++
|
imgArea.Min.Y++
|
||||||
@ -50,9 +58,13 @@ UP:
|
|||||||
|
|
||||||
RIGHT:
|
RIGHT:
|
||||||
for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- {
|
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++ {
|
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||||
if !colorIsBlank(img.At(x, y)) {
|
if !colorIsBlank(img.At(x, y)) {
|
||||||
break RIGHT
|
allowNonBlank--
|
||||||
|
if allowNonBlank <= 0 {
|
||||||
|
break RIGHT
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imgArea.Max.X--
|
imgArea.Max.X--
|
||||||
@ -60,9 +72,13 @@ RIGHT:
|
|||||||
|
|
||||||
BOTTOM:
|
BOTTOM:
|
||||||
for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- {
|
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++ {
|
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||||
if !colorIsBlank(img.At(x, y)) {
|
if !colorIsBlank(img.At(x, y)) {
|
||||||
break BOTTOM
|
allowNonBlank--
|
||||||
|
if allowNonBlank <= 0 {
|
||||||
|
break BOTTOM
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imgArea.Max.Y--
|
imgArea.Max.Y--
|
||||||
|
1
main.go
1
main.go
@ -114,6 +114,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
|||||||
ViewHeight: perfectHeight,
|
ViewHeight: perfectHeight,
|
||||||
Quality: cmd.Options.Quality,
|
Quality: cmd.Options.Quality,
|
||||||
Crop: cmd.Options.Crop,
|
Crop: cmd.Options.Crop,
|
||||||
|
CropRatio: cmd.Options.CropRatio,
|
||||||
Brightness: cmd.Options.Brightness,
|
Brightness: cmd.Options.Brightness,
|
||||||
Contrast: cmd.Options.Contrast,
|
Contrast: cmd.Options.Contrast,
|
||||||
AutoRotate: cmd.Options.AutoRotate,
|
AutoRotate: cmd.Options.AutoRotate,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user