Help us understand the problem. What is going on with this article?

go langでjpgからwebpに変換する

More than 3 years have passed since last update.

概要

go langでjpg形式のファイルをwebpに変換する。

前準備

go get github.com/chai2010/image

調査内容

サンプルだと、jpgファイルからwebpファイルに変換しているのだが、ファイルを経由して処理したくはないので、go lang が提供するインタフェースで使えるかを調査した。

内容

package webp

import (
    "image"
    "io"

    ImageExt "github.com/chai2010/image"
    _ "github.com/chai2010/image/jpeg"
    _ "github.com/chai2010/image/webp"
)

type Options struct {
    Lossless bool
    Quality  float32
}

func Decode(r io.Reader) (image.Image, error) {
    m, _, err := ImageExt.Decode(r)

    if err != nil {
        return nil, err
    }

    return m, err

}

func Encode(w io.Writer, i image.Image, options *Options) error {
    return ImageExt.Encode("webp", w, i, ImageExt.NewOptions(options.Lossless, options.Quality))
}

import (
    "github.com/masayukioguni/go-webp-sample/webp"
    "os"
    "path/filepath"
)

func main() {
    f, _ := os.Open("sample.jpg")
    defer f.Close()

    m, _ := webp.Decode(f)

    toimg, _ := os.Create("new.webp")
    defer toimg.Close()

    _ = webp.Encode(toimg, m, &webp.Options{false, 50})

}

まとめ

  • LosslessやQualityには別途調べる必要がある
  • いつかgo langに実装されそうな機能なので、go langにはいったらそっちを使いたい。
  • go langは楽しい

資料

変換ライブラリ
サンプルソース

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away