Skip to content

Commit

Permalink
Added aspect ratio option to covergen
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Oct 19, 2019
1 parent cde0ed5 commit 1b51d22
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
9 changes: 5 additions & 4 deletions covergen/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# covergen
Standalone tool to generate Kobo cover thumbnails. Useful for pre-generating thumbnails during import to save time or for when Kobo automatically generates the cover incorrectly (for example taking an image of the first page, or using a generic cover).
Standalone tool to generate Kobo cover thumbnails. Useful for pre-generating thumbnails during import to save time or for when Kobo automatically generates the cover incorrectly (for example taking an image of the first page, or using a generic cover). Covergen can also normalize the aspect ratio of covers for consistency.

Covergen is quite lenient about detecting cover images. The following methods are supported: meta[name=cover] with the path as the content, meta[name=cover] with a manifest id reference as the content, and manifest>item[properties=cover-image] with the image path as the href. Each detected path can be relative to the epub root or to the package document.

Expand All @@ -16,9 +16,10 @@ Version:
seriesmeta dev
Options:
-h, --help Show this help message
-m, --method string Resize algorithm to use (bilinear, bicubic, lanczos2, lanczos3) (default "lanczos3")
-r, --regenerate Re-generate all covers
-a, --aspect-ratio float Stretch the covers to fit a specific aspect ratio (for example 1.3, 1.5, 1.6)
-h, --help Show this help message
-m, --method string Resize algorithm to use (bilinear, bicubic, lanczos2, lanczos3) (default "lanczos3")
-r, --regenerate Re-generate all covers
Arguments:
KOBO_PATH is the path to the Kobo eReader. If not specified, covergen will try to automatically detect the Kobo.
Expand Down
44 changes: 30 additions & 14 deletions covergen/covergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"archive/zip"
"errors"
"fmt"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"os"
"path"
"path/filepath"
"reflect"
"strings"

"image"
_ "image/gif"
"image/jpeg"
_ "image/png"

"github.com/bamiaux/rez"
"github.com/beevik/etree"
"github.com/geek1011/koboutils/kobo"
Expand All @@ -26,6 +27,7 @@ var version = "dev"
func main() {
regenerate := pflag.BoolP("regenerate", "r", false, "Re-generate all covers")
method := pflag.StringP("method", "m", "lanczos3", "Resize algorithm to use (bilinear, bicubic, lanczos2, lanczos3)")
ar := pflag.Float64P("aspect-ratio", "a", 0, "Stretch the covers to fit a specific aspect ratio (for example 1.3, 1.5, 1.6)")
// TODO: invert, grayscale options
help := pflag.BoolP("help", "h", false, "Show this help message")
pflag.Parse()
Expand Down Expand Up @@ -95,6 +97,15 @@ func main() {
nn++
continue
}

if *ar != 0 {
origCover, err = stretch(origCover, filter, *ar)
if err != nil {
fmt.Fprintf(os.Stderr, "--------- Could not stretch cover: %v.\n", err)
ne++
continue
}
}
}

resized, err := resize(dev, ct, filter, origCover)
Expand Down Expand Up @@ -271,27 +282,32 @@ func extract(epub string) (image.Image, error) {
return img, nil // img may be nil
}

func resize(dev kobo.Device, ct kobo.CoverType, filter rez.Filter, orig image.Image) (image.Image, error) {
szo := orig.Bounds().Size()
szn := dev.CoverSized(ct, szo)

if szn.Eq(szo) {
return orig, nil
func dimens(img image.Image, filter rez.Filter, sz image.Point) (image.Image, error) {
if img.Bounds().Size().Eq(sz) {
return img, nil
}

origy, ok := orig.(*image.YCbCr)
imgy, ok := img.(*image.YCbCr)
if !ok {
return nil, fmt.Errorf("unsupported image encoding (not YCbCr): %s", reflect.TypeOf(orig))
return nil, fmt.Errorf("unsupported image encoding (not YCbCr): %s", reflect.TypeOf(img))
}

new := image.NewYCbCr(image.Rect(0, 0, szn.X, szn.Y), origy.SubsampleRatio)
if err := rez.Convert(new, orig, filter); err != nil {
new := image.NewYCbCr(image.Rect(0, 0, sz.X, sz.Y), imgy.SubsampleRatio)
if err := rez.Convert(new, img, filter); err != nil {
return nil, err
}

return new, nil
}

func resize(dev kobo.Device, ct kobo.CoverType, filter rez.Filter, orig image.Image) (image.Image, error) {
return dimens(orig, filter, dev.CoverSized(ct, orig.Bounds().Size()))
}

func stretch(orig image.Image, filter rez.Filter, ar float64) (image.Image, error) {
return dimens(orig, filter, image.Pt(orig.Bounds().Size().X, int(ar*float64(orig.Bounds().Size().X))))
}

func save(fn string, img image.Image, quality int) error {
if err := os.MkdirAll(path.Dir(filepath.ToSlash(fn)), 0755); err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@
<a href="https://github.com/geek1011/kepubify/releases/latest">here</a>. It will automatically detect your Kobo when you run it.
</p>
<p>
This tool is useful to save time or for when Kobo automatically generates the cover incorrectly (for example taking an image of the first page, or using a generic cover).
This tool is useful to save time or for when Kobo automatically generates the cover incorrectly (for example taking an image of
the first page, or using a generic cover). It can also optionally stretch the covers to a specific aspect ratio for consistency.
</p>
<p>
Covergen is quite lenient about detecting cover images. The following methods are supported: meta[name=cover] with the path as the content, meta[name=cover] with a manifest id reference as the content, and manifest&gt;item[properties=cover-image] with the image path as the href. Each detected path can be relative to the epub root or to the package document.
Covergen is quite lenient about detecting cover images. The following methods are supported: meta[name=cover] with the path as the
content, meta[name=cover] with a manifest id reference as the content, and manifest&gt;item[properties=cover-image] with the image
path as the href. Each detected path can be relative to the epub root or to the package document.
</p>
</div>
</div>
Expand Down

0 comments on commit 1b51d22

Please sign in to comment.