mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 07:42:37 +02:00
resize and use img only
This commit is contained in:
parent
7a9796000e
commit
dc24b757c7
@ -1,7 +1,6 @@
|
|||||||
package comicconverter
|
package comicconverter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
@ -13,7 +12,6 @@ import (
|
|||||||
type ComicConverter struct {
|
type ComicConverter struct {
|
||||||
Options ComicConverterOptions
|
Options ComicConverterOptions
|
||||||
img image.Image
|
img image.Image
|
||||||
grayImg *image.Gray16
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ComicConverterOptions struct {
|
type ComicConverterOptions struct {
|
||||||
@ -46,25 +44,28 @@ func (c *ComicConverter) GrayScale() *ComicConverter {
|
|||||||
panic("load image first")
|
panic("load image first")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.grayImg = image.NewGray16(c.img.Bounds())
|
grayImg := image.NewGray16(c.img.Bounds())
|
||||||
|
|
||||||
draw.Draw(c.grayImg, c.grayImg.Bounds(), c.img, image.Point{}, draw.Src)
|
draw.Draw(grayImg, grayImg.Bounds(), c.img, image.Point{}, draw.Src)
|
||||||
|
|
||||||
|
c.img = grayImg
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComicConverter) CropMarging() *ComicConverter {
|
func (c *ComicConverter) CropMarging() *ComicConverter {
|
||||||
if c.grayImg == nil {
|
if c.img == nil {
|
||||||
panic("grayscale first")
|
panic("load image first")
|
||||||
}
|
}
|
||||||
|
|
||||||
imgArea := c.grayImg.Bounds()
|
imgArea := c.img.Bounds()
|
||||||
colorLimit := color.Gray16{60000}
|
colorLimit := uint(color.Gray16{60000}.Y)
|
||||||
|
|
||||||
LEFT:
|
LEFT:
|
||||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||||
if c.grayImg.Gray16At(x, y).Y < colorLimit.Y {
|
cc, _, _, _ := color.Gray16Model.Convert(c.img.At(x, y)).RGBA()
|
||||||
|
if cc < uint32(colorLimit) {
|
||||||
break LEFT
|
break LEFT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,7 +75,8 @@ LEFT:
|
|||||||
UP:
|
UP:
|
||||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||||
if c.grayImg.Gray16At(x, y).Y < colorLimit.Y {
|
cc, _, _, _ := color.Gray16Model.Convert(c.img.At(x, y)).RGBA()
|
||||||
|
if cc < uint32(colorLimit) {
|
||||||
break UP
|
break UP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,7 +86,8 @@ 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-- {
|
||||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||||
if c.grayImg.Gray16At(x, y).Y < colorLimit.Y {
|
cc, _, _, _ := color.Gray16Model.Convert(c.img.At(x, y)).RGBA()
|
||||||
|
if cc < uint32(colorLimit) {
|
||||||
break RIGHT
|
break RIGHT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,37 +97,42 @@ 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-- {
|
||||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||||
if c.grayImg.Gray16At(x, y).Y < colorLimit.Y {
|
cc, _, _, _ := color.Gray16Model.Convert(c.img.At(x, y)).RGBA()
|
||||||
|
if cc < uint32(colorLimit) {
|
||||||
break BOTTOM
|
break BOTTOM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imgArea.Max.Y--
|
imgArea.Max.Y--
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("CROP", imgArea)
|
grayImg := image.NewGray16(image.Rectangle{
|
||||||
|
Min: image.Point{0, 0},
|
||||||
|
Max: image.Point{imgArea.Dx(), imgArea.Dy()},
|
||||||
|
})
|
||||||
|
|
||||||
c.grayImg = c.grayImg.SubImage(imgArea).(*image.Gray16)
|
draw.Draw(grayImg, grayImg.Bounds(), c.img, imgArea.Min, draw.Src)
|
||||||
|
|
||||||
|
c.img = grayImg
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComicConverter) Resize(w, h int) *ComicConverter {
|
func (c *ComicConverter) Resize(w, h int) *ComicConverter {
|
||||||
if c.grayImg == nil {
|
if c.img == nil {
|
||||||
panic("grayscale first")
|
panic("load image first")
|
||||||
}
|
}
|
||||||
|
|
||||||
dim := c.grayImg.Bounds()
|
dim := c.img.Bounds()
|
||||||
origWidth := dim.Dx()
|
origWidth := dim.Dx()
|
||||||
origHeight := dim.Dy()
|
origHeight := dim.Dy()
|
||||||
|
|
||||||
width, heigth := origWidth*h/origHeight, origHeight*w/origWidth
|
width, heigth := origWidth*h/origHeight, origHeight*w/origWidth
|
||||||
|
|
||||||
fmt.Println("W:", origWidth, width, h)
|
if width > w {
|
||||||
fmt.Println("H:", origHeight, heigth, w)
|
width = w
|
||||||
if width > origWidth {
|
}
|
||||||
width = origWidth
|
if heigth > h {
|
||||||
} else if heigth > origHeight {
|
heigth = h
|
||||||
heigth = origHeight
|
|
||||||
}
|
}
|
||||||
|
|
||||||
imgGray := image.NewGray16(image.Rectangle{
|
imgGray := image.NewGray16(image.Rectangle{
|
||||||
@ -132,18 +140,16 @@ func (c *ComicConverter) Resize(w, h int) *ComicConverter {
|
|||||||
Max: image.Point{width, heigth},
|
Max: image.Point{width, heigth},
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("RESIZE", imgGray.Bounds())
|
draw.BiLinear.Scale(imgGray, imgGray.Bounds(), c.img, c.img.Bounds(), draw.Src, nil)
|
||||||
|
|
||||||
draw.BiLinear.Scale(imgGray, imgGray.Bounds(), c.grayImg, c.grayImg.Bounds(), draw.Src, nil)
|
c.img = imgGray
|
||||||
|
|
||||||
c.grayImg = imgGray
|
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComicConverter) Save(output string) *ComicConverter {
|
func (c *ComicConverter) Save(output string) *ComicConverter {
|
||||||
if c.grayImg == nil {
|
if c.img == nil {
|
||||||
panic("grayscale first")
|
panic("load image first")
|
||||||
}
|
}
|
||||||
o, err := os.Create(output)
|
o, err := os.Create(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -156,7 +162,7 @@ func (c *ComicConverter) Save(output string) *ComicConverter {
|
|||||||
quality = c.Options.Quality
|
quality = c.Options.Quality
|
||||||
}
|
}
|
||||||
|
|
||||||
err = jpeg.Encode(o, c.grayImg, &jpeg.Options{Quality: quality})
|
err = jpeg.Encode(o, c.img, &jpeg.Options{Quality: quality})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user