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

golangでImageMagickを触りたい

More than 3 years have passed since last update.

golangでImageMagickを触りたい

これ使う。
https://github.com/gographics/imagick

debianな人は libmagickwand-dev が必要なので入れておく。
redhatな人は ImageMagick-devel こっち。

MagickWand の API ラッパーなので基本他と一緒。
なのでいったん他の言語(phpとか)で試してからgolangに書き下すといいかんじ。

サンプル

  • base.jpg の上に
  • overlay.png100x100 にリサイズして
  • ど真ん中に乗せて
  • png フォーマットで
  • stdout に出力する
  • と同時に out.png にも出力
package main

import (
    "fmt"
    "os"

    "github.com/gographics/imagick/imagick"
)

func main() {
    imagick.Initialize()
    defer imagick.Terminate()

    // 下地
    mw1 := imagick.NewMagickWand()
    defer mw1.Destroy()
    err := mw1.ReadImage("base.jpg")
    if err != nil {
        panic(err)
    }
    w1 := mw1.GetImageWidth()
    h1 := mw1.GetImageHeight()

    // 上モノ
    mw2 := imagick.NewMagickWand()
    defer mw2.Destroy()
    err = mw2.ReadImage("overlay.png")
    if err != nil {
        panic(err)
    }
    // リサイズして
    err = mw2.ResizeImage(100, 100, imagick.FILTER_POINT, 0)
    if err != nil {
        panic(err)
    }
    w2 := mw2.GetImageWidth()
    h2 := mw2.GetImageHeight()

    // ど真ん中に乗せる
    err = mw1.CompositeImage(mw2, mw2.GetImageCompose(), int((w1-w2)/2), int((h1-h2)/2))
    if err != nil {
        panic(err)
    }

    err = mw1.SetFormat("png")
    if err != nil {
        panic(err)
    }
    // stdoutに吐く
    blob := mw1.GetImageBlob()
    os.Stdout.Write(blob)

    // ファイルにも吐く
    mw1.WriteImage("out.png")

    fmt.Fprintln(os.Stderr, "FINISH!!")
}
arc279
フリーランスだけどわりとどこ行っても「いい感じにする」のに定評があるおっさん
Why not register and get more from Qiita?
  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
No 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
ユーザーは見つかりませんでした