mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 16:22:37 +02:00
Compare commits
5 Commits
0263a64321
...
721d373a7f
Author | SHA1 | Date | |
---|---|---|---|
721d373a7f | |||
2b7582e776 | |||
4dc1f7275f | |||
a1b7497f0c | |||
0637d64b93 |
@ -98,6 +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.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.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.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")
|
||||
|
@ -24,6 +24,10 @@ type Options struct {
|
||||
Profile string `yaml:"profile"`
|
||||
Quality int `yaml:"quality"`
|
||||
Crop bool `yaml:"crop"`
|
||||
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"`
|
||||
Contrast int `yaml:"contrast"`
|
||||
Auto bool `yaml:"-"`
|
||||
@ -59,6 +63,10 @@ func New() *Options {
|
||||
Profile: "",
|
||||
Quality: 85,
|
||||
Crop: true,
|
||||
CropRatioLeft: 1,
|
||||
CropRatioUp: 1,
|
||||
CropRatioRight: 1,
|
||||
CropRatioBottom: 3,
|
||||
Brightness: 0,
|
||||
Contrast: 0,
|
||||
AutoRotate: false,
|
||||
@ -159,6 +167,7 @@ func (o *Options) ShowConfig() string {
|
||||
View : %s
|
||||
Quality : %d
|
||||
Crop : %v
|
||||
CropRatio : %d Left - %d Up - %d Right - %d Bottom
|
||||
Brightness : %d
|
||||
Contrast : %d
|
||||
AutoRotate : %v
|
||||
@ -174,6 +183,7 @@ func (o *Options) ShowConfig() string {
|
||||
viewDesc,
|
||||
o.Quality,
|
||||
o.Crop,
|
||||
o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom,
|
||||
o.Brightness,
|
||||
o.Contrast,
|
||||
o.AutoRotate,
|
||||
|
@ -237,6 +237,7 @@ func (e *ePub) Write() error {
|
||||
Description: "Writing Part",
|
||||
CurrentJob: 2,
|
||||
TotalJob: 2,
|
||||
Quiet: e.Quiet,
|
||||
})
|
||||
for i, part := range epubParts {
|
||||
ext := filepath.Ext(e.Output)
|
||||
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
Rotate image if the source width > height.
|
||||
*/
|
||||
package epubfilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
func AutoRotate() gift.Filter {
|
||||
return &autoRotateFilter{}
|
||||
}
|
||||
|
||||
type autoRotateFilter struct {
|
||||
}
|
||||
|
||||
func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
|
||||
if srcBounds.Dx() > srcBounds.Dy() {
|
||||
dstBounds = gift.Rotate90().Bounds(srcBounds)
|
||||
} else {
|
||||
dstBounds = srcBounds
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
|
||||
if src.Bounds().Dx() > src.Bounds().Dy() {
|
||||
gift.Rotate90().Draw(dst, src, options)
|
||||
} else {
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package epubimage
|
||||
|
||||
import (
|
||||
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// create filter to apply to the source
|
||||
func NewGift(options *Options) *gift.GIFT {
|
||||
g := gift.New()
|
||||
g.SetParallelization(false)
|
||||
|
||||
if options.AutoRotate {
|
||||
g.Add(epubfilters.AutoRotate())
|
||||
}
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
g.Add(
|
||||
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
epubfilters.Pixel(),
|
||||
)
|
||||
return g
|
||||
}
|
||||
|
||||
// create filters to cut image into 2 equal pieces
|
||||
func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
|
||||
gifts := make([]*gift.GIFT, 2)
|
||||
|
||||
gifts[0] = gift.New(
|
||||
epubfilters.CropSplitDoublePage(options.Manga),
|
||||
)
|
||||
|
||||
gifts[1] = gift.New(
|
||||
epubfilters.CropSplitDoublePage(!options.Manga),
|
||||
)
|
||||
|
||||
for _, g := range gifts {
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
|
||||
g.Add(
|
||||
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
)
|
||||
}
|
||||
|
||||
return gifts
|
||||
}
|
@ -3,6 +3,10 @@ package epubimage
|
||||
// options for image transformation
|
||||
type Options struct {
|
||||
Crop bool
|
||||
CropRatioLeft int
|
||||
CropRatioUp int
|
||||
CropRatioRight int
|
||||
CropRatioBottom int
|
||||
ViewWidth int
|
||||
ViewHeight int
|
||||
Quality int
|
||||
|
70
internal/epub/imagefilters/epub_image_filters.go
Normal file
70
internal/epub/imagefilters/epub_image_filters.go
Normal file
@ -0,0 +1,70 @@
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// create filter to apply to the source
|
||||
func NewGift(img image.Image, options *epubimage.Options) *gift.GIFT {
|
||||
g := gift.New()
|
||||
g.SetParallelization(false)
|
||||
|
||||
if options.Crop {
|
||||
g.Add(AutoCrop(
|
||||
img,
|
||||
options.CropRatioLeft,
|
||||
options.CropRatioUp,
|
||||
options.CropRatioRight,
|
||||
options.CropRatioBottom,
|
||||
))
|
||||
}
|
||||
if options.AutoRotate && img.Bounds().Dx() > img.Bounds().Dy() {
|
||||
g.Add(gift.Rotate90())
|
||||
}
|
||||
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
|
||||
g.Add(
|
||||
Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
Pixel(),
|
||||
)
|
||||
return g
|
||||
}
|
||||
|
||||
// create filters to cut image into 2 equal pieces
|
||||
func NewGiftSplitDoublePage(options *epubimage.Options) []*gift.GIFT {
|
||||
gifts := make([]*gift.GIFT, 2)
|
||||
|
||||
gifts[0] = gift.New(
|
||||
CropSplitDoublePage(options.Manga),
|
||||
)
|
||||
|
||||
gifts[1] = gift.New(
|
||||
CropSplitDoublePage(!options.Manga),
|
||||
)
|
||||
|
||||
for _, g := range gifts {
|
||||
g.SetParallelization(false)
|
||||
if options.Contrast != 0 {
|
||||
g.Add(gift.Contrast(float32(options.Contrast)))
|
||||
}
|
||||
if options.Brightness != 0 {
|
||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||
}
|
||||
|
||||
g.Add(
|
||||
Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||
)
|
||||
}
|
||||
|
||||
return gifts
|
||||
}
|
88
internal/epub/imagefilters/epub_image_filters_autocrop.go
Normal file
88
internal/epub/imagefilters/epub_image_filters_autocrop.go
Normal file
@ -0,0 +1,88 @@
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// Lookup for margin and crop
|
||||
func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter {
|
||||
return gift.Crop(
|
||||
findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}),
|
||||
)
|
||||
}
|
||||
|
||||
// check if the color is blank enough
|
||||
func colorIsBlank(c color.Color) bool {
|
||||
g := color.GrayModel.Convert(c).(color.Gray)
|
||||
return g.Y >= 0xe0
|
||||
}
|
||||
|
||||
// lookup for margin (blank) around the image
|
||||
type cutRatioOptions struct {
|
||||
Left, Up, Right, Bottom int
|
||||
}
|
||||
|
||||
func findMarging(img image.Image, cutRatio cutRatioOptions) image.Rectangle {
|
||||
imgArea := img.Bounds()
|
||||
|
||||
LEFT:
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
allowNonBlank := imgArea.Dy() * cutRatio.Left / 100
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
allowNonBlank--
|
||||
if allowNonBlank <= 0 {
|
||||
break LEFT
|
||||
}
|
||||
}
|
||||
}
|
||||
imgArea.Min.X++
|
||||
}
|
||||
|
||||
UP:
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
allowNonBlank := imgArea.Dx() * cutRatio.Up / 100
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
allowNonBlank--
|
||||
if allowNonBlank <= 0 {
|
||||
break UP
|
||||
}
|
||||
}
|
||||
}
|
||||
imgArea.Min.Y++
|
||||
}
|
||||
|
||||
RIGHT:
|
||||
for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- {
|
||||
allowNonBlank := imgArea.Dy() * cutRatio.Right / 100
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
allowNonBlank--
|
||||
if allowNonBlank <= 0 {
|
||||
break RIGHT
|
||||
}
|
||||
}
|
||||
}
|
||||
imgArea.Max.X--
|
||||
}
|
||||
|
||||
BOTTOM:
|
||||
for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- {
|
||||
allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
allowNonBlank--
|
||||
if allowNonBlank <= 0 {
|
||||
break BOTTOM
|
||||
}
|
||||
}
|
||||
}
|
||||
imgArea.Max.Y--
|
||||
}
|
||||
|
||||
return imgArea
|
||||
}
|
@ -1,7 +1,4 @@
|
||||
/*
|
||||
Create a title with the cover image
|
||||
*/
|
||||
package epubfilters
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
@ -14,6 +11,7 @@ import (
|
||||
"golang.org/x/image/font/gofont/gomonobold"
|
||||
)
|
||||
|
||||
// Create a title with the cover image
|
||||
func CoverTitle(title string) gift.Filter {
|
||||
return &coverTitle{title}
|
||||
}
|
@ -1,9 +1,4 @@
|
||||
/*
|
||||
cut a double page in 2 part: left and right.
|
||||
|
||||
this will cut in the middle of the page.
|
||||
*/
|
||||
package epubfilters
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
@ -12,6 +7,8 @@ import (
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// Cut a double page in 2 part: left and right.
|
||||
// This will cut in the middle of the page.
|
||||
func CropSplitDoublePage(right bool) gift.Filter {
|
||||
return &cropSplitDoublePage{right}
|
||||
}
|
@ -1,9 +1,4 @@
|
||||
/*
|
||||
generate a blank pixel 1x1, if the size of the image is 0x0.
|
||||
|
||||
An image 0x0 is not a valid image, and failed to read.
|
||||
*/
|
||||
package epubfilters
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
@ -13,6 +8,8 @@ import (
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// Generate a blank pixel 1x1, if the size of the image is 0x0.
|
||||
// An image 0x0 is not a valid image, and failed to read.
|
||||
func Pixel() gift.Filter {
|
||||
return &pixel{}
|
||||
}
|
@ -1,9 +1,4 @@
|
||||
/*
|
||||
Resize image by keeping aspect ratio.
|
||||
|
||||
This will reduce or enlarge image to fit into the viewWidth and viewHeight.
|
||||
*/
|
||||
package epubfilters
|
||||
package epubimagefilters
|
||||
|
||||
import (
|
||||
"image"
|
||||
@ -12,6 +7,8 @@ import (
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
// Resize image by keeping aspect ratio.
|
||||
// This will reduce or enlarge image to fit into the viewWidth and viewHeight.
|
||||
func Resize(viewWidth, viewHeight int, resampling gift.Resampling) gift.Filter {
|
||||
return &resizeFilter{
|
||||
viewWidth, viewHeight, resampling,
|
@ -14,9 +14,9 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
|
||||
epubimagefilters "github.com/celogeek/go-comic-converter/v2/internal/epub/imagefilters"
|
||||
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
|
||||
"github.com/disintegration/gift"
|
||||
_ "golang.org/x/image/webp"
|
||||
@ -29,6 +29,17 @@ type tasks struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// only accept jpg, png and webp as source file
|
||||
func isSupportedImage(path string) bool {
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".jpg", ".jpeg", ".png", ".webp":
|
||||
{
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// extract and convert images
|
||||
func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
||||
images := make([]*epubimage.Image, 0)
|
||||
@ -101,15 +112,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if o.Image.Crop {
|
||||
g := gift.New(gift.Crop(findMarging(src)))
|
||||
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
|
||||
g.Draw(newSrc, src)
|
||||
src = newSrc
|
||||
}
|
||||
|
||||
g := epubimage.NewGift(o.Image)
|
||||
|
||||
g := epubimagefilters.NewGift(src, o.Image)
|
||||
// Convert image
|
||||
dst := image.NewGray(g.Bounds(src.Bounds()))
|
||||
g.Draw(dst, src)
|
||||
@ -138,7 +141,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
||||
if (!o.Image.HasCover || img.Id > 0) &&
|
||||
o.Image.AutoSplitDoublePage &&
|
||||
src.Bounds().Dx() > src.Bounds().Dy() {
|
||||
gifts := epubimage.NewGiftSplitDoublePage(o.Image)
|
||||
gifts := epubimagefilters.NewGiftSplitDoublePage(o.Image)
|
||||
for i, g := range gifts {
|
||||
part := i + 1
|
||||
dst := image.NewGray(g.Bounds(src.Bounds()))
|
||||
@ -187,7 +190,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
|
||||
// create a title page with the cover
|
||||
func LoadCoverTitleData(img *epubimage.Image, title string, quality int) *epubimagedata.ImageData {
|
||||
// Create a blur version of the cover
|
||||
g := gift.New(epubfilters.CoverTitle(title))
|
||||
g := gift.New(epubimagefilters.CoverTitle(title))
|
||||
dst := image.NewGray(g.Bounds(img.Raw.Bounds()))
|
||||
g.Draw(dst, img.Raw)
|
||||
|
||||
|
@ -1,72 +0,0 @@
|
||||
package epubimageprocessing
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// only accept jpg, png and webp as source file
|
||||
func isSupportedImage(path string) bool {
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".jpg", ".jpeg", ".png", ".webp":
|
||||
{
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// check if the color is blank enough
|
||||
func colorIsBlank(c color.Color) bool {
|
||||
g := color.GrayModel.Convert(c).(color.Gray)
|
||||
return g.Y >= 0xf0
|
||||
}
|
||||
|
||||
// lookup for margin (blank) around the image
|
||||
func findMarging(img image.Image) image.Rectangle {
|
||||
imgArea := img.Bounds()
|
||||
|
||||
LEFT:
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
break LEFT
|
||||
}
|
||||
}
|
||||
imgArea.Min.X++
|
||||
}
|
||||
|
||||
UP:
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
break UP
|
||||
}
|
||||
}
|
||||
imgArea.Min.Y++
|
||||
}
|
||||
|
||||
RIGHT:
|
||||
for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- {
|
||||
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
break RIGHT
|
||||
}
|
||||
}
|
||||
imgArea.Max.X--
|
||||
}
|
||||
|
||||
BOTTOM:
|
||||
for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- {
|
||||
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
|
||||
if !colorIsBlank(img.At(x, y)) {
|
||||
break BOTTOM
|
||||
}
|
||||
}
|
||||
imgArea.Max.Y--
|
||||
}
|
||||
|
||||
return imgArea
|
||||
}
|
4
main.go
4
main.go
@ -114,6 +114,10 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
ViewHeight: perfectHeight,
|
||||
Quality: cmd.Options.Quality,
|
||||
Crop: cmd.Options.Crop,
|
||||
CropRatioLeft: cmd.Options.CropRatioLeft,
|
||||
CropRatioUp: cmd.Options.CropRatioUp,
|
||||
CropRatioRight: cmd.Options.CropRatioRight,
|
||||
CropRatioBottom: cmd.Options.CropRatioBottom,
|
||||
Brightness: cmd.Options.Brightness,
|
||||
Contrast: cmd.Options.Contrast,
|
||||
AutoRotate: cmd.Options.AutoRotate,
|
||||
|
Loading…
x
Reference in New Issue
Block a user