18
0

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 1 year has passed since last update.

Elixirでテスト用の画像を量産しよう

Last updated at Posted at 2022-11-30

テストデータで大量の画像を作りたいと思ったことがありました。
1枚1枚画像処理ソフトで作るのは面倒くさいので、Elixirで大量に作りたいと思います。

今回使うライブラリー

mogrify

まず動作確認

iexを立ち上げる

$ iex

mogrifyを使う準備

下記を打ち込んで準備します

Mix.install([{:mogrify, "~> 0.9.2"}])
import Mogrify

実行例

Erlang/OTP 25 [erts-13.0.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Mix.install([{:mogrify, "~> 0.9.2"}])
:ok

iex(2)> import Mogrify
Mogrify

画像をつくってみよう

iex(6)> %Mogrify.Image{path: "hoge.png", ext: "png"} |> custom("size", "640x480") |> custom("background", "#FFFFFF") |> custom("fill", "#000000") |> custom("pango", "hogehoge") |> create(path: ".")

実行結果

%Mogrify.Image{
  animated: false,
  buffer: nil,
  dirty: %{},
  ext: "",
  format: nil,
  frame_count: 1,
  height: nil,
  operations: [],
  path: ".",
  width: nil
}

出力されるファイル

hoge.png

画像ファイルを大量に作ってみよう

プロジェクトを新規作成
コマンドラインから

$ mix new hoge

実行結果

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/hoge.ex
* creating test
* creating test/test_helper.exs
* creating test/hoge_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd hoge
    mix test

ディレクトリの移動と画像格納用のディレクトリ作成

$ cd hoge
$ mkdir img

mix.exsを編集
{:mogrify, "~> 0.9.2"} をdepsに追加

mix.exs
defmodule Hoge.MixProject do
  use Mix.Project

  def project do
    [
      app: :hoge,
      version: "0.1.0",
      elixir: "~> 1.13",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
+     {:mogrify, "~> 0.9.2"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

ライブラリーを取得

$ mix deps.get

実行結果

Resolving Hex dependencies...
Dependency resolution completed:
Unchanged:
  mogrify 0.9.2
All dependencies are up to date

lib/hoge.exを編集
10枚の画像を作る関数を作成します

lib/hoge.ex
defmodule Hoge do
  import Mogrify

  @moduledoc """
  Documentation for `Hoge`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Hoge.hello()
      :world

  """
  def hello do
    :world
  end

+ def exec() do
+   1..10 |> Enum.each(&create_img/1)
+ end

+ def create_img(no) do
+   %Mogrify.Image{path: "hoge#{no}.png", ext: "png"}
+   |> custom("size", "640x480")
+   |> custom("background", "#FFFFFF")
+   |> custom("fill", "#000000")
+   |> custom("pango", "hogehoge#{no}")
+   |> create(path: "./img/")
+ end

end

実行してみましょう

$ iex -S mix

iex上で

iex(1)> Hoge.exec()

実行結果

:ok

ファイル一覧してみる

iex(5)> ls("img")

実行結果

hoge1.png      hoge10.png     hoge2.png      hoge3.png      hoge4.png      hoge5.png      
hoge6.png      hoge7.png      hoge8.png      hoge9.png  

実際作成された画像(10枚作られる)
hoge1.png
      :
      :
hoge10.png

画像ファイルを作ることができました

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?