add kingpin commands

This commit is contained in:
Milan Nikolic
2015-11-04 12:58:26 +01:00
parent 82e8a6cbac
commit d7be2f4d5e
2 changed files with 104 additions and 68 deletions

View File

@@ -28,34 +28,55 @@ Download
Using Using
----- -----
usage: cbconvert [<flags>] <args>... usage: cbconvert [<flags>] <command> [<args> ...]
Comic Book convert tool. Comic Book convert tool.
Flags: Flags:
--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 -o, --outdir="." Output directory
-b, --bmp encode images to 4-Bit BMP (16 colors) instead of JPEG -m, --size=0 Process only files larger then size (in MB)
-g, --gif encode images to GIF instead of JPEG -R, --recursive Process subdirectories recursively
-w, --width=0 image width -Q, --quiet Hide console output
-h, --height=0 image height
-q, --quality=75 JPEG image quality
-f, --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
-N, --no_rgb do not convert images that have RGB colorspace
-I, --no_nonimage remove non image files from archive
-s, --suffix=SUFFIX add suffix to file basename
-c, --cover extract cover
-t, --thumbnail extract cover thumbnail (freedesktop spec.)
-o, --outdir="." output directory
-m, --size=0 process only files larger then size (in MB)
-G, --grayscale convert images to grayscale (monochromatic)
-R, --recursive process subdirectories recursively
-Q, --quiet hide console output
Args: Args:
<args> filename or directory <args> filename or directory
Commands:
help [<command>...]
Show help.
convert [<flags>] <args>...
Convert archive or document (default)
-w, --width=0 Image width
-h, --height=0 Image height
-q, --quality=75 JPEG image quality
-f, --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
-p, --png Encode images to PNG 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
-N, --rgb Convert images that have RGB colorspace (use --no-rgb if you only want to process grayscale images)
-I, --nonimage Leave non image files in archive (use --no-nonimage to remove non image files from archive)
-G, --grayscale Convert images to grayscale (monochromatic)
-s, --suffix=SUFFIX Add suffix to file basename
cover [<flags>] <args>...
Extract cover
-w, --width=0 Image width
-h, --height=0 Image height
-q, --quality=75 JPEG image quality
-f, --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
thumbnail [<flags>] <args>...
Extract cover thumbnail (freedesktop spec.)
-w, --width=0 Image width
-h, --height=0 Image height
Examples Examples
-------- --------
@@ -69,9 +90,13 @@ 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, Lanczos algorithm is used for resizing: Generate thumbnails by freedesktop specification in ~/.thumbnails/normal directory with width 512:
cbconvert --filter=7 --outdir ~/.thumbnails/normal --thumbnail /media/comics/GrooTheWanderer/ cbconvert thumbnail --width 512 --outdir ~/.thumbnails/normal /media/comics/GrooTheWanderer/
Extract covers to ~/covers dir for all supported files found in directory, Lanczos algorithm is used for resizing:
cbconvert cover --outdir ~/covers --filter=7 /media/comics/GrooTheWanderer/
Compile Compile
------- -------

View File

@@ -90,8 +90,8 @@ type options struct {
Width int // image width Width int // image width
Height int // image height Height int // image height
Filter int // 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos Filter int // 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos
NoRGB bool // do not convert images that have RGB colorspace RGB bool // convert images that have RGB colorspace
NoNonImage bool // remove non image files from archive NonImage bool // Leave non image files in archive
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.)
@@ -316,7 +316,7 @@ func convertArchive(file string) {
continue continue
} }
if opts.NoRGB && !isGrayScale(img) { if !opts.RGB && !isGrayScale(img) {
copyFile(bytes.NewReader(buf), filepath.Join(workdir, filepath.Base(pathname))) copyFile(bytes.NewReader(buf), filepath.Join(workdir, filepath.Base(pathname)))
continue continue
} }
@@ -327,7 +327,7 @@ func convertArchive(file string) {
go convertImage(img, 0, pathname) go convertImage(img, 0, pathname)
} }
} else { } else {
if !opts.NoNonImage { if opts.NonImage {
copyFile(bytes.NewReader(buf), filepath.Join(workdir, filepath.Base(pathname))) copyFile(bytes.NewReader(buf), filepath.Join(workdir, filepath.Base(pathname)))
} }
} }
@@ -366,7 +366,7 @@ func convertDirectory(path string) {
continue continue
} }
if opts.NoRGB && !isGrayScale(i) { if !opts.RGB && !isGrayScale(i) {
copyFile(f, filepath.Join(workdir, filepath.Base(img))) copyFile(f, filepath.Join(workdir, filepath.Base(img)))
continue continue
} }
@@ -754,10 +754,6 @@ func extractCover(file string, info os.FileInfo) {
cover = imaging.Resize(cover, opts.Width, opts.Height, filters[opts.Filter]) cover = imaging.Resize(cover, opts.Width, opts.Height, filters[opts.Filter])
} }
if opts.Grayscale {
cover = imaging.Grayscale(cover)
}
filename := filepath.Join(opts.Outdir, fmt.Sprintf("%s.jpg", getBasename(file))) filename := filepath.Join(opts.Outdir, fmt.Sprintf("%s.jpg", getBasename(file)))
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@@ -792,10 +788,6 @@ func extractThumbnail(file string, info os.FileInfo) {
cover = imaging.Resize(cover, 256, 0, filters[opts.Filter]) cover = imaging.Resize(cover, 256, 0, filters[opts.Filter])
} }
if opts.Grayscale {
cover = imaging.Grayscale(cover)
}
imagick.Initialize() imagick.Initialize()
mw := imagick.NewMagickWand() mw := imagick.NewMagickWand()
@@ -842,26 +834,45 @@ func parseFlags() {
opts = options{} opts = options{}
kingpin.Version("CBconvert 0.3.0") kingpin.Version("CBconvert 0.3.0")
kingpin.CommandLine.Help = "Comic Book convert tool." kingpin.CommandLine.Help = "Comic Book convert tool."
kingpin.Flag("png", "encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG) kingpin.UsageTemplate(kingpin.CompactUsageTemplate)
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("outdir", "Output directory").Default(".").Short('o').StringVar(&opts.Outdir)
kingpin.Flag("width", "image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width) kingpin.Flag("size", "Process only files larger then size (in MB)").Short('m').Default(strconv.Itoa(0)).Int64Var(&opts.Size)
kingpin.Flag("height", "image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height) kingpin.Flag("recursive", "Process subdirectories recursively").Short('R').BoolVar(&opts.Recursive)
kingpin.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) kingpin.Flag("quiet", "Hide console output").Short('Q').BoolVar(&opts.Quiet)
kingpin.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Short('f').
Default(strconv.Itoa(Linear)).IntVar(&opts.Filter) convert := kingpin.Command("convert", "Convert archive or document (default)").Default()
kingpin.Flag("no_rgb", "do not convert images that have RGB colorspace").Short('N').BoolVar(&opts.NoRGB) convert.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments)
kingpin.Flag("no_nonimage", "remove non image files from archive").Short('I').BoolVar(&opts.NoNonImage) convert.Flag("width", "Image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width)
kingpin.Flag("suffix", "add suffix to file basename").Short('s').StringVar(&opts.Suffix) convert.Flag("height", "Image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height)
kingpin.Flag("cover", "extract cover").Short('c').BoolVar(&opts.Cover) convert.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality)
kingpin.Flag("thumbnail", "extract cover thumbnail (freedesktop spec.)").Short('t').BoolVar(&opts.Thumbnail) convert.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Short('f').Default(strconv.Itoa(Linear)).IntVar(&opts.Filter)
kingpin.Flag("outdir", "output directory").Default(".").Short('o').StringVar(&opts.Outdir) convert.Flag("png", "Encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG)
kingpin.Flag("size", "process only files larger then size (in MB)").Short('m').Default(strconv.Itoa(0)).Int64Var(&opts.Size) convert.Flag("bmp", "Encode images to 4-Bit BMP (16 colors) instead of JPEG").Short('b').BoolVar(&opts.ToBMP)
kingpin.Flag("grayscale", "convert images to grayscale (monochromatic)").Short('G').BoolVar(&opts.Grayscale) convert.Flag("gif", "Encode images to GIF instead of JPEG").Short('g').BoolVar(&opts.ToGIF)
kingpin.Flag("recursive", "process subdirectories recursively").Short('R').BoolVar(&opts.Recursive) convert.Flag("rgb", "Convert images that have RGB colorspace (use --no-rgb if you only want to process grayscale images)").Short('N').Default("true").BoolVar(&opts.RGB)
kingpin.Flag("quiet", "hide console output").Short('Q').BoolVar(&opts.Quiet) convert.Flag("nonimage", "Leave non image files in archive (use --no-nonimage to remove non image files from archive)").Short('I').Default("true").BoolVar(&opts.NonImage)
kingpin.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments) convert.Flag("grayscale", "Convert images to grayscale (monochromatic)").Short('G').BoolVar(&opts.Grayscale)
kingpin.Parse() convert.Flag("suffix", "Add suffix to file basename").Short('s').StringVar(&opts.Suffix)
cover := kingpin.Command("cover", "Extract cover")
cover.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments)
cover.Flag("width", "Image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width)
cover.Flag("height", "Image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height)
cover.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality)
cover.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Short('f').Default(strconv.Itoa(Linear)).IntVar(&opts.Filter)
thumbnail := kingpin.Command("thumbnail", "Extract cover thumbnail (freedesktop spec.)")
thumbnail.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments)
thumbnail.Flag("width", "Image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width)
thumbnail.Flag("height", "Image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height)
switch kingpin.Parse() {
case "cover":
opts.Cover = true
case "thumbnail":
opts.Thumbnail = true
}
} }
func main() { func main() {