use imaging instead of resize

This commit is contained in:
Milan Nikolic
2015-11-03 21:39:30 +01:00
parent 55b53efcca
commit 0c6b9fcaa8
2 changed files with 64 additions and 41 deletions

View File

@@ -13,7 +13,7 @@ Features
- always saves processed comic in CBZ (ZIP) archive format - always saves processed comic in CBZ (ZIP) archive format
- images can be converted to JPEG, PNG, GIF or 4-Bit BMP (16 colors) file format - images can be converted to JPEG, PNG, GIF or 4-Bit BMP (16 colors) file format
- reads JPEG, PNG, BMP, GIF, TIFF and WEBP file formats - reads JPEG, PNG, BMP, GIF, TIFF and WEBP file formats
- choose resize algorithm (NearestNeighbor, Bilinear, Bicubic, MitchellNetravali, Lanczos2/3) - choose resize algorithm (NearestNeighbor, Box, Linear, MitchellNetravali, CatmullRom, Gaussian, Lanczos)
- export covers from comics - export covers from comics
- create thumbnails from covers by [freedesktop](http://www.freedesktop.org/wiki/) specification - create thumbnails from covers by [freedesktop](http://www.freedesktop.org/wiki/) specification
@@ -36,13 +36,13 @@ Using
--help Show context-sensitive help (also try --help-long and --help-man). --help Show context-sensitive help (also try --help-long and --help-man).
--version Show application version. --version Show application version.
-p, --png encode images to PNG instead of JPEG -p, --png encode images to PNG instead of JPEG
-b, --bmp encode images to 4-Bit BMP instead of JPEG -b, --bmp encode images to 4-Bit BMP (16 colors) instead of JPEG
-g, --gif encode images to GIF instead of JPEG -g, --gif encode images to GIF instead of JPEG
-w, --width=0 image width -w, --width=0 image width
-h, --height=0 image height -h, --height=0 image height
-q, --quality=75 JPEG image quality -q, --quality=75 JPEG image quality
-n, --norgb do not convert images with RGB colorspace -n, --norgb do not convert images with RGB colorspace
-r, --resize=1 0=NearestNeighbor, 1=Bilinear, 2=Bicubic, 3=MitchellNetravali, 4=Lanczos2, 5=Lanczos3 -f, --filter=0 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
-s, --suffix=SUFFIX add suffix to file basename -s, --suffix=SUFFIX add suffix to file basename
-c, --cover extract cover -c, --cover extract cover
-t, --thumbnail extract cover thumbnail (freedesktop spec.) -t, --thumbnail extract cover thumbnail (freedesktop spec.)
@@ -54,7 +54,6 @@ Using
Args: Args:
<args> filename or directory <args> filename or directory
Examples Examples
-------- --------
@@ -68,9 +67,9 @@ Convert all images in archive to 4bit BMP image and save result in ~/comics dire
[BMP](http://en.wikipedia.org/wiki/BMP_file_format) format is very good choice for black&white pages. Archive size can be smaller 2-3x and file will be readable by comic readers. [BMP](http://en.wikipedia.org/wiki/BMP_file_format) format is very good choice for black&white pages. Archive size can be smaller 2-3x and file will be readable by comic readers.
Generate thumbnails by freedesktop specification in ~/.thumbnails/normal directory, Lanczos3 algorithm is used for resizing: Generate thumbnails by freedesktop specification in ~/.thumbnails/normal directory, Lanczos algorithm is used for resizing:
cbconvert --resize=5 --outdir ~/.thumbnails/normal --thumbnail /media/comics/GrooTheWanderer/ cbconvert --filter=7 --outdir ~/.thumbnails/normal --thumbnail /media/comics/GrooTheWanderer/
Compile Compile
------- -------
@@ -100,11 +99,11 @@ Compile unarr library:
Install dependencies: Install dependencies:
go get github.com/cheggaaa/pb go get github.com/cheggaaa/pb
go get github.com/disintegration/imaging
go get github.com/gen2brain/go-fitz go get github.com/gen2brain/go-fitz
go get github.com/gen2brain/go-unarr go get github.com/gen2brain/go-unarr
go get github.com/gographics/imagick/imagick go get github.com/gographics/imagick/imagick
go get github.com/hotei/bmp go get github.com/hotei/bmp
go get github.com/nfnt/resize
go get github.com/skarademir/naturalsort go get github.com/skarademir/naturalsort
go get golang.org/x/image/tiff go get golang.org/x/image/tiff
go get golang.org/x/image/webp go get golang.org/x/image/webp

View File

@@ -40,17 +40,39 @@ import (
"syscall" "syscall"
"github.com/cheggaaa/pb" "github.com/cheggaaa/pb"
"github.com/disintegration/imaging"
"github.com/gen2brain/go-fitz" "github.com/gen2brain/go-fitz"
"github.com/gen2brain/go-unarr" "github.com/gen2brain/go-unarr"
"github.com/gographics/imagick/imagick" "github.com/gographics/imagick/imagick"
_ "github.com/hotei/bmp" _ "github.com/hotei/bmp"
"github.com/nfnt/resize"
"github.com/skarademir/naturalsort" "github.com/skarademir/naturalsort"
_ "golang.org/x/image/tiff" _ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
) )
// Resample filters
const (
NearestNeighbor int = iota // Fastest resampling filter, no antialiasing
Box // Box filter (averaging pixels)
Linear // Bilinear filter, smooth and reasonably fast
MitchellNetravali // А smooth bicubic filter
CatmullRom // A sharp bicubic filter
Gaussian // Blurring filter that uses gaussian function, useful for noise removal
Lanczos // High-quality resampling filter, it's slower than cubic filters
)
var filters = map[int]imaging.ResampleFilter{
NearestNeighbor: imaging.NearestNeighbor,
Box: imaging.Box,
Linear: imaging.Linear,
MitchellNetravali: imaging.MitchellNetravali,
CatmullRom: imaging.CatmullRom,
Gaussian: imaging.Gaussian,
Lanczos: imaging.Lanczos,
}
// Globals
var ( var (
opts options opts options
workdir string workdir string
@@ -66,9 +88,9 @@ type options struct {
ToGIF bool // encode images to GIF instead of JPEG ToGIF bool // encode images to GIF instead of JPEG
Quality int // JPEG image quality Quality int // JPEG image quality
NoRGB bool // do not convert images with RGB colorspace NoRGB bool // do not convert images with RGB colorspace
Width uint // image width Width int // image width
Height uint // image height Height int // image height
Resize int // 0=NearestNeighbor, 1=Bilinear, 2=Bicubic, 3=MitchellNetravali, 4=Lanczos2, 5=Lanczos3 Filter int // 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
Suffix string // add suffix to file basename Suffix string // add suffix to file basename
Cover bool // extract cover Cover bool // extract cover
Thumbnail bool // extract cover thumbnail (freedesktop spec.) Thumbnail bool // extract cover thumbnail (freedesktop spec.)
@@ -106,8 +128,7 @@ func convertImage(img image.Image, index int, pathName string) {
var i image.Image var i image.Image
if opts.Width > 0 || opts.Height > 0 { if opts.Width > 0 || opts.Height > 0 {
i = resize.Resize(opts.Width, opts.Height, img, i = imaging.Resize(img, opts.Width, opts.Height, filters[opts.Filter])
resize.InterpolationFunction(opts.Resize))
} else { } else {
i = img i = img
} }
@@ -139,13 +160,14 @@ func convertImage(img image.Image, index int, pathName string) {
w.SetColor("black") w.SetColor("black")
defer w.Destroy() defer w.Destroy()
mw.SetImageFormat("BMP3")
mw.SetImageBackgroundColor(w) mw.SetImageBackgroundColor(w)
mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE)
mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
mw.SetImageMatte(false) mw.SetImageMatte(false)
mw.SetImageCompression(imagick.COMPRESSION_NO) mw.SetImageCompression(imagick.COMPRESSION_NO)
mw.QuantizeImage(16, imagick.COLORSPACE_SRGB, 8, true, true) mw.QuantizeImage(16, imagick.COLORSPACE_SRGB, 8, true, true)
mw.WriteImage(fmt.Sprintf("BMP3:%s", filename)) mw.WriteImage(filename)
} else if opts.ToGIF { } else if opts.ToGIF {
// convert image to GIF // convert image to GIF
imagick.Initialize() imagick.Initialize()
@@ -160,7 +182,12 @@ func convertImage(img image.Image, index int, pathName string) {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error ReadImageBlob: %v\n", err.Error()) fmt.Fprintf(os.Stderr, "Error ReadImageBlob: %v\n", err.Error())
} }
mw.SetImageFormat("gif") mw.SetImageFormat("GIF")
mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE)
mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
mw.SetImageMatte(false)
mw.SetImageCompression(imagick.COMPRESSION_LZW)
mw.QuantizeImage(16, imagick.COLORSPACE_SRGB, 8, true, true)
mw.WriteImage(filename) mw.WriteImage(filename)
} else { } else {
// convert image to JPEG (default) // convert image to JPEG (default)
@@ -704,8 +731,7 @@ func extractCover(file string, info os.FileInfo) {
} }
if opts.Width > 0 || opts.Height > 0 { if opts.Width > 0 || opts.Height > 0 {
cover = resize.Resize(opts.Width, opts.Height, cover, cover = imaging.Resize(cover, opts.Width, opts.Height, filters[opts.Filter])
resize.InterpolationFunction(opts.Resize))
} }
filename := filepath.Join(opts.Outdir, fmt.Sprintf("%s.jpg", getBasename(file))) filename := filepath.Join(opts.Outdir, fmt.Sprintf("%s.jpg", getBasename(file)))
@@ -737,11 +763,9 @@ func extractThumbnail(file string, info os.FileInfo) {
} }
if opts.Width > 0 || opts.Height > 0 { if opts.Width > 0 || opts.Height > 0 {
cover = resize.Resize(opts.Width, opts.Height, cover, cover = imaging.Resize(cover, opts.Width, opts.Height, filters[opts.Filter])
resize.InterpolationFunction(opts.Resize))
} else { } else {
cover = resize.Resize(256, 0, cover, cover = imaging.Resize(cover, 256, 0, filters[opts.Filter])
resize.InterpolationFunction(opts.Resize))
} }
imagick.Initialize() imagick.Initialize()
@@ -760,7 +784,7 @@ func extractThumbnail(file string, info os.FileInfo) {
fileuri := "file://" + file fileuri := "file://" + file
filename := filepath.Join(opts.Outdir, fmt.Sprintf("%x.png", md5.Sum([]byte(fileuri)))) filename := filepath.Join(opts.Outdir, fmt.Sprintf("%x.png", md5.Sum([]byte(fileuri))))
mw.SetImageFormat("png") mw.SetImageFormat("PNG")
mw.SetImageProperty("Software", "cbconvert") mw.SetImageProperty("Software", "cbconvert")
mw.SetImageProperty("Description", "Thumbnail of "+fileuri) mw.SetImageProperty("Description", "Thumbnail of "+fileuri)
mw.SetImageProperty("Thumb::URI", fileuri) mw.SetImageProperty("Thumb::URI", fileuri)
@@ -793,12 +817,12 @@ func parseFlags() {
kingpin.Flag("png", "encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG) kingpin.Flag("png", "encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG)
kingpin.Flag("bmp", "encode images to 4-Bit BMP (16 colors) instead of JPEG").Short('b').BoolVar(&opts.ToBMP) kingpin.Flag("bmp", "encode images to 4-Bit BMP (16 colors) instead of JPEG").Short('b').BoolVar(&opts.ToBMP)
kingpin.Flag("gif", "encode images to GIF instead of JPEG").Short('g').BoolVar(&opts.ToGIF) kingpin.Flag("gif", "encode images to GIF instead of JPEG").Short('g').BoolVar(&opts.ToGIF)
kingpin.Flag("width", "image width").Default(strconv.Itoa(0)).Short('w').UintVar(&opts.Width) kingpin.Flag("width", "image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width)
kingpin.Flag("height", "image height").Default(strconv.Itoa(0)).Short('h').UintVar(&opts.Height) kingpin.Flag("height", "image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height)
kingpin.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) kingpin.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality)
kingpin.Flag("norgb", "do not convert images with RGB colorspace").Short('n').BoolVar(&opts.NoRGB) kingpin.Flag("norgb", "do not convert images with RGB colorspace").Short('n').BoolVar(&opts.NoRGB)
kingpin.Flag("resize", "0=NearestNeighbor, 1=Bilinear, 2=Bicubic, 3=MitchellNetravali, 4=Lanczos2, 5=Lanczos3").Short('r'). kingpin.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Short('f').
Default(strconv.Itoa(int(resize.Bilinear))).IntVar(&opts.Resize) Default(strconv.Itoa(NearestNeighbor)).IntVar(&opts.Filter)
kingpin.Flag("suffix", "add suffix to file basename").Short('s').StringVar(&opts.Suffix) kingpin.Flag("suffix", "add suffix to file basename").Short('s').StringVar(&opts.Suffix)
kingpin.Flag("cover", "extract cover").Short('c').BoolVar(&opts.Cover) kingpin.Flag("cover", "extract cover").Short('c').BoolVar(&opts.Cover)
kingpin.Flag("thumbnail", "extract cover thumbnail (freedesktop spec.)").Short('t').BoolVar(&opts.Thumbnail) kingpin.Flag("thumbnail", "extract cover thumbnail (freedesktop spec.)").Short('t').BoolVar(&opts.Thumbnail)