init project

This commit is contained in:
Celogeek 2022-12-25 16:28:48 +01:00
commit 892c0ca320
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
3 changed files with 142 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module go-comic-converter
go 1.19
require golang.org/x/image v0.2.0

26
go.sum Normal file
View File

@ -0,0 +1,26 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.2.0 h1:/DcQ0w3VHKCC5p0/P2B0JpAZ9Z++V2KOo2fyU89CXBQ=
golang.org/x/image v0.2.0/go.mod h1:la7oBXb9w3YFjBqaAwtynVioc1ZvOnNteUNrifGNmAI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

111
main.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
"golang.org/x/image/draw"
)
type KindleSpec struct {
Width int
Height int
}
func (k *KindleSpec) Bounds() image.Rectangle {
return image.Rectangle{
Min: image.Point{0, 0},
Max: image.Point{k.Width, k.Height},
}
}
func (k *KindleSpec) ConvertGray16(img image.Image) image.Image {
r := detectMarging(img)
width, heigth := r.Dx()*k.Height/r.Dy(), r.Dy()*k.Width/r.Dx()
if width > k.Width {
width = k.Width
} else {
heigth = k.Height
}
imgGray := image.NewGray16(image.Rectangle{
Min: image.Point{0, 0},
Max: image.Point{width, heigth},
})
draw.BiLinear.Scale(imgGray, imgGray.Bounds(), img, r, draw.Src, nil)
return imgGray
}
var KindleScribe = &KindleSpec{1860, 2480}
func lineIsBlank(img image.Image, y int) bool {
for x := 0; x < img.Bounds().Max.X; x++ {
r, _, _, _ := img.At(x, y).RGBA()
if r < 0xfff {
return false
}
}
return true
}
func colIsBlank(img image.Image, x int) bool {
for y := 0; y < img.Bounds().Max.Y; y++ {
r, _, _, _ := img.At(x, y).RGBA()
if r < 0xfff { // allow a light gray, white = 0xffff
return false
}
}
return true
}
func detectMarging(img image.Image) image.Rectangle {
rect := img.Bounds()
var xmin, xmax = rect.Min.X, rect.Max.X - 1
var ymin, ymax = rect.Min.Y, rect.Max.Y - 1
for ; ymin < ymax && lineIsBlank(img, ymin); ymin++ {
rect.Min.Y++
}
for ; ymin < ymax && lineIsBlank(img, ymax); ymax-- {
rect.Max.Y--
}
for ; xmin < xmax && colIsBlank(img, xmin); xmin++ {
rect.Min.X++
}
for ; xmin < xmax && colIsBlank(img, xmax); xmax-- {
rect.Max.X--
}
fmt.Println(rect)
return rect
}
func main() {
f, err := os.Open("/Users/vincent/Downloads/00001.jpg")
if err != nil {
panic(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
panic(err)
}
imgGray := KindleScribe.ConvertGray16(img)
o, err := os.Create("/Users/vincent/Downloads/00001_gray.jpg")
if err != nil {
panic(err)
}
defer o.Close()
err = jpeg.Encode(o, imgGray, &jpeg.Options{Quality: 90})
if err != nil {
panic(err)
}
}