improve crop ratio

This commit is contained in:
Celogeek 2023-04-26 18:22:33 +02:00
parent a1b7497f0c
commit 4dc1f7275f
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 37 additions and 13 deletions

View File

@ -98,7 +98,10 @@ 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.CropRatioLeft, "crop-ratio-left", c.Options.CropRatioLeft, "Crop ratio left: ratio of pixels allow to be non blank while cutting on the left.")
c.AddIntParam(&c.Options.CropRatioUp, "crop-ratio-up", c.Options.CropRatioUp, "Crop ratio up: ratio of pixels allow to be non blank while cutting on the top.")
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.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")

View File

@ -24,7 +24,10 @@ 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"` CropRatioLeft int `yaml:"crop_ratio_left"`
CropRatioUp int `yaml:"crop_ratio_up"`
CropRatioRight int `yaml:"crop_ratio_right"`
CropRatioBottom int `yaml:"crop_ratio_bottom"`
Brightness int `yaml:"brightness"` Brightness int `yaml:"brightness"`
Contrast int `yaml:"contrast"` Contrast int `yaml:"contrast"`
Auto bool `yaml:"-"` Auto bool `yaml:"-"`
@ -60,7 +63,10 @@ func New() *Options {
Profile: "", Profile: "",
Quality: 85, Quality: 85,
Crop: true, Crop: true,
CropRatio: 5, CropRatioLeft: 1,
CropRatioUp: 1,
CropRatioRight: 1,
CropRatioBottom: 3,
Brightness: 0, Brightness: 0,
Contrast: 0, Contrast: 0,
AutoRotate: false, AutoRotate: false,
@ -161,7 +167,7 @@ func (o *Options) ShowConfig() string {
View : %s View : %s
Quality : %d Quality : %d
Crop : %v Crop : %v
CropRatio : %d CropRatio : %d Left - %d Up - %d Right - %d Bottom
Brightness : %d Brightness : %d
Contrast : %d Contrast : %d
AutoRotate : %v AutoRotate : %v
@ -177,7 +183,7 @@ func (o *Options) ShowConfig() string {
viewDesc, viewDesc,
o.Quality, o.Quality,
o.Crop, o.Crop,
o.CropRatio, o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom,
o.Brightness, o.Brightness,
o.Contrast, o.Contrast,
o.AutoRotate, o.AutoRotate,

View File

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

View File

@ -102,7 +102,12 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
} }
if o.Image.Crop { if o.Image.Crop {
g := gift.New(gift.Crop(findMarging(src, o.Image.CropRatio))) g := gift.New(gift.Crop(findMarging(src, cutRatioOptions{
Left: o.Image.CropRatioLeft,
Up: o.Image.CropRatioUp,
Right: o.Image.CropRatioRight,
Bottom: o.Image.CropRatioBottom,
})))
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

View File

@ -25,12 +25,16 @@ func colorIsBlank(c color.Color) bool {
} }
// lookup for margin (blank) around the image // lookup for margin (blank) around the image
func findMarging(img image.Image, cutRatio int) image.Rectangle { type cutRatioOptions struct {
Left, Up, Right, Bottom int
}
func findMarging(img image.Image, cutRatio cutRatioOptions) 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 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)) {
allowNonBlank-- allowNonBlank--
@ -44,7 +48,7 @@ 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 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)) {
allowNonBlank-- allowNonBlank--
@ -58,7 +62,7 @@ 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 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)) {
allowNonBlank-- allowNonBlank--
@ -72,7 +76,7 @@ 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 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)) {
allowNonBlank-- allowNonBlank--

View File

@ -114,7 +114,10 @@ $ 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, CropRatioLeft: cmd.Options.CropRatioLeft,
CropRatioUp: cmd.Options.CropRatioUp,
CropRatioRight: cmd.Options.CropRatioRight,
CropRatioBottom: cmd.Options.CropRatioBottom,
Brightness: cmd.Options.Brightness, Brightness: cmd.Options.Brightness,
Contrast: cmd.Options.Contrast, Contrast: cmd.Options.Contrast,
AutoRotate: cmd.Options.AutoRotate, AutoRotate: cmd.Options.AutoRotate,