ConvertToJpeg
package main
import (
"flag"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"log"
"os"
)
func main() {
var (
src = flag.String("s", "", "JPEG に変換したい画像パス")
dest = flag.String("d", "", "JEPG に変換した画像パス")
quality = flag.Int("q", 100, "JPEG 変換時の Quality")
)
flag.Parse()
convertToJpeg(*src, *dest, *quality)
}
func convertToJpeg(src string, dest string, quality int) {
file, err := os.Open(src)
logError(err)
defer file.Close()
img, _, err := image.Decode(file)
logError(err)
out, err := os.Create(dest)
logError(err)
defer out.Close()
opts := &jpeg.Options{Quality: quality}
jpeg.Encode(out, img, opts)
}
func logError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}