8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Go で画像を JPEG に変換

Posted at
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)
	}
}

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?