LoginSignup
0
0

More than 1 year has passed since last update.

goで画像の色調変換の処理を行う

Last updated at Posted at 2022-10-11

はじめに

こんにちは、icemint0828です。
今回は自作パッケージの紹介も兼ねて、goでの画像の色調変換の処理の方法を紹介します。

動作環境

go 1.18.3

使用パッケージ

  • icemint0828/imgedit
    画像の色調変換の処理に使用しています。
    画像ファイルの拡張子の変換にも使用しています。

インストール

$  go get github.com/icemint0828/imgedit@v1.6.0

画像の色調変換の処理

main.go
package main

import (
	"github.com/icemint0828/imgedit"
)

func main() {
	// FileConverter
	fc, _, err := imgedit.NewFileConverter("srcImage.png")
	if err != nil {
		panic(err)
	}
    // 色調変換(imgedit.GrayModel, imgedit.SepiaModelで指定)
	fc.Filter(imgedit.GrayModel)
	
	// 保存(jpeg, gif形式での保存も可能)
	err = fc.SaveAs("dstImage.png", imgedit.Png)
	if err != nil {
		panic(err)
	}
}

色調はグレースケールとセピアの2種類が用意されています。
後述しますが独自の色調変換を行う事も可能です。

fc.SaveAsの第二引数では以下の形式で保存するファイルの形式を選択出来ます。

  • imgedit.Png
  • imgedit.Jpeg
  • imgedit.Gif

画像の色調変換の処理(image取得)

main.go
package main

import (
	"image"

	"github.com/icemint0828/imgedit"
)

func main() {
	// srcImgを何らかの方法で取得
	var srcImg image.Image

	// FileConverter
	c := imgedit.NewConverter(srcImg)

    // 色調変換(imgedit.GrayModel, imgedit.SepiaModelで指定)
	c.Filter(imgedit.GrayModel)
	dstImg := c.Convert()

	// dstImgを使用
}

ファイルを介さずにimageを処理したい場合は、上記のようにimgedit.NewConverterで直接画像の加工が出来ます。

パッケージ内の処理の説明

imgeditパッケージ内のconverter.goで画像の色調変換の処理を実施しています。

converter.go
// Filter change the image color to specified color model
// GrayModel, SepiaModel
func (c *converter) Filter(filterModel FilterModel) {
	if filterModel == nil {
		return
	}

	dst := image.NewRGBA(image.Rect(0, 0, c.Bounds().Dx(), c.Bounds().Dy()))
	dstSize := dst.Bounds().Size()
	alphaModel := color.AlphaModel
	for x := 0; x < dstSize.X; x++ {
		for y := 0; y < dstSize.Y; y++ {
			dstColor := c.Image.At(x, y)
			_, _, _, a := dstColor.RGBA()
			if a <= 0 {
				dst.Set(x, y, alphaModel.Convert(dstColor))
			} else {
				dst.Set(x, y, filterModel.Convert(dstColor))
			}
		}
	}
	c.Image = dst
}

引数から色調変換のモデルを取得し、*image.RGBAに対して、各ピクセルの色情報の変換を行った後に書き込みしています。
α値(透過度)が0のものは色調変換時に予期せぬ色調になる事があるため、別途標準パッケージで定義されているcolor.AlphaModelにて変換を行っています。
imgedit.FilterModel型は標準パッケージのcolor.Model型のラッパーとなっています。

converter.go
// FilterModel wrapper
type FilterModel color.Model

// GrayModel convert image to gray
var GrayModel = FilterModel(color.GrayModel)

// SepiaModel convert image to sepia
var SepiaModel = FilterModel(color.ModelFunc(sepiaModel))

func sepiaModel(c color.Color) color.Color {
	// once converted to GRAY, then to SEPIA, we can get a beautiful conversion.
	grayColor := color.GrayModel.Convert(c)
	r, g, b, a := grayColor.RGBA()

	r = uint32(float64(r) * (float64(240) / float64(255)))
	g = uint32(float64(g) * (float64(200) / float64(255)))
	b = uint32(float64(b) * (float64(148) / float64(255)))
	return color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8), A: uint8(a >> 8)}
}

imgedit.SepiaModelを参考にr,g,bの値を変化させる事で独自の色調変換の実施が可能となります。

おわりに

簡単な内容とはなりますが、画像の色調変換の処理を紹介させて頂きました。
最後まで読んで頂き、ありがとうございました。

前の記事

goで画像の反転処理を行う

0
0
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
0
0