9
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 Image について、最近機能が追加され、一部の記述が簡素化できるようになったので、いくつか紹介します

以前書いた記事

追加された Trailing bang (末尾の !)

以下の2つが追加されています

  • Image.from_binary! : 0.25.0 で追加
  • Image.to_nx! : 0.27.0 で追加

これにより、以下のように簡略化できます

ryo_img =
  "https://www.elixirconf.eu/assets/images/ryo-wakabayashi.png"
  |> Req.get!()
  |> Map.get(:body)
- |> Image.from_binary()
- |> elem(1)
+ |> Image.from_binary!()
- {:ok, nx_ryo_img} =  Image.to_nx(ryo_img)
- Kino.Image.new(nx_ryo_img)
+ ryo_img
+ |> Image.to_nx!()
+ |> Kino.Image.new()

:ok と :error で場合わけしたい場合は ! なしの方を使うべきですが、明示的にエラーを発生させたい場合は ! ありの方が有用なエラーメッセージが表示されます

Livebook の UI (Kino) からの変換の簡素化

0.27.0 で Image.from_kino が追加されました

これによって、 Livebook のファイル選択から Image の画像形式に変換する処理が簡素化されます

ただし、 Kino.Input.image:format:rgb である必要があります

chroma_key_img_input = Kino.Input.image("CHROMA KEY IMAGE", format: :rgb)

chroma_key_img =
  chroma_key_img_input
  |> Kino.Input.read()
- |> Map.get(:data)
- |> Image.from_binary!()
+ |> Image.from_kino!()

Image.Kino.show(chroma_key_img, max_height: 400)

SVG 読込の明示化

0.32.0 で追加された Image.from_svg で SVG ファイルの読込が分かりやすく明示化されました

svg_img =
  "https://www.elixirconf.eu/assets/images/drops.svg"
  |> Req.get!()
  |> Map.get(:body)
- |> Image.from_binary!()
+ |> Image.from_svg!()

まとめ

ちょっとした変更ですが、可読性が上がるのが嬉しいですね

9
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
9
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?