Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Generate image with mogrify

notebooks/mogrify.livemd

Generate image with mogrify

Mix.install([
  {:kino, "~> 0.8.0"},
  {:mogrify, "~> 0.9.2"},
  {:nx, "~> 0.4.0"},
  {:stb_image, "~> 0.5"}
])

Resources

ImageMagick

{image_magic_info, 0} = System.shell("magick -version")
IO.puts(image_magic_info)
{supported_formats, 0} = System.shell("magick -list format")
IO.puts(supported_formats)

Current directory

File.cwd!()

Image directory

img_dir = Path.expand("./tmp/img/")

Generate an image

img_name = "test.png"

# create a PNG image
%Mogrify.Image{path: img_name, ext: "png"}
|> Mogrify.custom("size", "280x280")
|> Mogrify.custom("seed", 10)
|> Mogrify.custom("plasma", "fractal")
|> Mogrify.create(path: img_dir)
|> dbg()

img_full_path = Path.join([img_dir, img_name]) |> Path.expand()
IO.puts(img_full_path)

# verify the generated image
File.read!(img_full_path)
|> Kino.Image.new("image/png")

Heatmap an image

  • pango is not working on macos for some reason…
img_dir = "./tmp/img/"
img_name = "toukon.png"

import Mogrify

# create a PNG image
%Mogrify.Image{path: img_name, ext: "png"}
|> Mogrify.custom("size", "40x20")
|> Mogrify.custom("background", "#000000")
|> Mogrify.custom("fill", "#FFFFFF")
|> Mogrify.custom("pango", "闘魂")
|> Mogrify.create(path: img_dir)
|> dbg()

img_full_path = Path.join([img_dir, img_name]) |> Path.expand()
IO.puts(img_full_path)

nx_image =
  File.read!(img_full_path)
  |> StbImage.read_binary!()
  |> StbImage.to_nx()

nx_image[channels: 0]
|> Nx.to_heatmap()