diff --git a/README.md b/README.md index 3d1dc01..e4d2ac5 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Features - 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 - choose resize algorithm (NearestNeighbor, Box, Linear, MitchellNetravali, CatmullRom, Gaussian, Lanczos) + - rotate, flip or grayscale images - export covers from comics - create thumbnails from covers by [freedesktop](http://www.freedesktop.org/wiki/) specification @@ -33,12 +34,12 @@ Using Comic Book convert tool. Flags: - --help Show context-sensitive help (also try --help-long and --help-man). - --version Show application version. - -o, --outdir="." Output directory - -m, --size=0 Process only files larger then size (in MB) - -R, --recursive Process subdirectories recursively - -Q, --quiet Hide console output + --help Show context-sensitive help (also try --help-long and --help-man). + --version Show application version. + --outdir="." Output directory + --size=0 Process only files larger then size (in MB) + --recursive Process subdirectories recursively + --quiet Hide console output Args: filename or directory @@ -49,33 +50,35 @@ Using convert [] ... - Convert archive or document (default) + Convert archive or document (default command) - -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 + --width=0 Image width + --height=0 Image height + --quality=75 JPEG image quality + --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos + --png Encode images to PNG instead of JPEG + --bmp Encode images to 4-Bit BMP (16 colors) instead of JPEG + --gif Encode images to GIF instead of JPEG + --rgb Convert images that have RGB colorspace (use --no-rgb if you only want to process grayscale images) + --nonimage Leave non image files in archive (use --no-nonimage to remove non image files from archive) + --grayscale Convert images to grayscale (monochromatic) + --rotate=0 Rotate images, valid values are 0, 90, 180, 270 + --flip="none" Flip images, valid values are none, horizontal, vertical + --suffix=SUFFIX Add suffix to file basename cover [] ... 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 + --width=0 Image width + --height=0 Image height + --quality=75 JPEG image quality + --filter=2 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos thumbnail [] ... Extract cover thumbnail (freedesktop spec.) - -w, --width=0 Image width - -h, --height=0 Image height + --width=0 Image width + --height=0 Image height Examples -------- @@ -84,9 +87,9 @@ Rescale images to 1200px for all supported files found in directory with size la cbconvert --recursive --width 1200 --size 60 /media/comics/Thorgal/ -Convert all images in archive to 4bit BMP image and save result in ~/comics directory: +Convert all images in pdf to 4bit BMP image and save result in ~/comics directory: - cbconvert --bmp --outdir ~/comics /media/comics/Garfield/Garfield_01.cbz + cbconvert --bmp --outdir ~/comics /media/comics/Garfield/Garfield_01.pdf [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. diff --git a/cbconvert.go b/cbconvert.go index 3fefb60..2f14077 100644 --- a/cbconvert.go +++ b/cbconvert.go @@ -97,6 +97,8 @@ type options struct { Thumbnail bool // extract cover thumbnail (freedesktop spec.) Outdir string // output directory Grayscale bool // convert images to grayscale (monochromatic) + Rotate int // Rotate images, valid values are 0, 90, 180, 270 + Flip string // Flip images, valid values are none, horizontal, vertical Recursive bool // process subdirectories recursively Size int64 // process only files larger then size (in MB) Quiet bool // hide console output @@ -140,6 +142,26 @@ func convertImage(img image.Image, index int, pathName string) { i = imaging.Grayscale(img) } + if opts.Rotate > 0 { + switch opts.Rotate { + case 90: + i = imaging.Rotate90(img) + case 180: + i = imaging.Rotate180(img) + case 270: + i = imaging.Rotate270(img) + } + } + + if opts.Flip != "none" { + switch opts.Flip { + case "horizontal": + i = imaging.FlipH(img) + case "vertical": + i = imaging.FlipV(img) + } + } + if opts.ToPNG { // convert image to PNG f, err := os.Create(filename) @@ -836,36 +858,38 @@ func parseFlags() { kingpin.CommandLine.Help = "Comic Book convert tool." kingpin.UsageTemplate(kingpin.CompactUsageTemplate) - kingpin.Flag("outdir", "Output directory").Default(".").Short('o').StringVar(&opts.Outdir) - kingpin.Flag("size", "Process only files larger then size (in MB)").Short('m').Default(strconv.Itoa(0)).Int64Var(&opts.Size) - kingpin.Flag("recursive", "Process subdirectories recursively").Short('R').BoolVar(&opts.Recursive) - kingpin.Flag("quiet", "Hide console output").Short('Q').BoolVar(&opts.Quiet) + kingpin.Flag("outdir", "Output directory").Default(".").StringVar(&opts.Outdir) + kingpin.Flag("size", "Process only files larger then size (in MB)").Default(strconv.Itoa(0)).Int64Var(&opts.Size) + kingpin.Flag("recursive", "Process subdirectories recursively").BoolVar(&opts.Recursive) + kingpin.Flag("quiet", "Hide console output").BoolVar(&opts.Quiet) - convert := kingpin.Command("convert", "Convert archive or document (default)").Default() + convert := kingpin.Command("convert", "Convert archive or document (default command)").Default() convert.Arg("args", "filename or directory").Required().ExistingFilesOrDirsVar(&arguments) - convert.Flag("width", "Image width").Default(strconv.Itoa(0)).Short('w').IntVar(&opts.Width) - convert.Flag("height", "Image height").Default(strconv.Itoa(0)).Short('h').IntVar(&opts.Height) - convert.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) - 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) - convert.Flag("png", "Encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG) - convert.Flag("bmp", "Encode images to 4-Bit BMP (16 colors) instead of JPEG").Short('b').BoolVar(&opts.ToBMP) - convert.Flag("gif", "Encode images to GIF instead of JPEG").Short('g').BoolVar(&opts.ToGIF) - 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) - 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) - convert.Flag("grayscale", "Convert images to grayscale (monochromatic)").Short('G').BoolVar(&opts.Grayscale) - convert.Flag("suffix", "Add suffix to file basename").Short('s').StringVar(&opts.Suffix) + convert.Flag("width", "Image width").Default(strconv.Itoa(0)).IntVar(&opts.Width) + convert.Flag("height", "Image height").Default(strconv.Itoa(0)).IntVar(&opts.Height) + convert.Flag("quality", "JPEG image quality").Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) + convert.Flag("filter", "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 6=Gaussian, 7=Lanczos").Default(strconv.Itoa(Linear)).IntVar(&opts.Filter) + convert.Flag("png", "Encode images to PNG instead of JPEG").BoolVar(&opts.ToPNG) + convert.Flag("bmp", "Encode images to 4-Bit BMP (16 colors) instead of JPEG").BoolVar(&opts.ToBMP) + convert.Flag("gif", "Encode images to GIF instead of JPEG").BoolVar(&opts.ToGIF) + convert.Flag("rgb", "Convert images that have RGB colorspace (use --no-rgb if you only want to process grayscale images)").Default("true").BoolVar(&opts.RGB) + convert.Flag("nonimage", "Leave non image files in archive (use --no-nonimage to remove non image files from archive)").Default("true").BoolVar(&opts.NonImage) + convert.Flag("grayscale", "Convert images to grayscale (monochromatic)").BoolVar(&opts.Grayscale) + convert.Flag("rotate", "Rotate images, valid values are 0, 90, 180, 270").Default(strconv.Itoa(0)).IntVar(&opts.Rotate) + convert.Flag("flip", "Flip images, valid values are none, horizontal, vertical").Default("none").StringVar(&opts.Flip) + convert.Flag("suffix", "Add suffix to file basename").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) + cover.Flag("width", "Image width").Default(strconv.Itoa(0)).IntVar(&opts.Width) + cover.Flag("height", "Image height").Default(strconv.Itoa(0)).IntVar(&opts.Height) + cover.Flag("quality", "JPEG image quality").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").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) + thumbnail.Flag("width", "Image width").Default(strconv.Itoa(0)).IntVar(&opts.Width) + thumbnail.Flag("height", "Image height").Default(strconv.Itoa(0)).IntVar(&opts.Height) switch kingpin.Parse() { case "cover":