Skip to content

Instantly share code, notes, and snippets.

@zacky1972
Last active April 19, 2023 10:33
Show Gist options
  • Save zacky1972/c029e2c1999d36f67ac2d408af82bd86 to your computer and use it in GitHub Desktop.
Save zacky1972/c029e2c1999d36f67ac2d408af82bd86 to your computer and use it in GitHub Desktop.
Horizontal Image Concatenation
System.put_env("EVISION_PREFER_PRECOMPILED", "true") # Remove if you use a platform on which Evision does not provide a pre-compiled library.
System.put_env("EVISION_PRECOMPILED_CACHE_DIR", "#{System.user_home!()}/.cache")
Mix.install(
[
{:nx, "~> 0.3"},
{:exla, "~> 0.3"},
{:evision, "~> 0.1.2", github: "cocoa-xu/evision", tag: "v0.1.2"}
],
config: [
nx: [default_backend: EXLA.Backend]
]
)
src_file = "ZACKY-3000.jpg"
src_file_ext = Path.extname(src_file)
src_file_basename = Path.basename(src_file, src_file_ext)
dst_file = "ZACKY-3000_d.jpg"
# image will be trimmed by shape after concatenation
shape = {3000, 3000, 3} # {image_height, image_width, num_channel}
# src_files is generated by Stream with counting up
src_files =
Stream.unfold(0, fn counter -> {counter, counter + 1} end)
|> Stream.map(& "#{src_file_basename}_#{&1}#{src_file_ext}")
|> Stream.take_while(& File.exists?(&1))
src_files
|> Enum.map(& Evision.imread!(&1))
|> Enum.map(& Evision.Nx.to_nx(&1))
|> Nx.concatenate() # here is the most important
|> Nx.slice([0, 0, 0], Tuple.to_list(shape)) # here is the trimming code.
|> then(& Evision.Nx.to_mat!(&1))
|> then(& Evision.imwrite!(dst_file, &1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment