Last active
August 31, 2022 07:32
-
-
Save zacky1972/ab48d2cea7a9aecd8d8cefa00c2b2ba5 to your computer and use it in GitHub Desktop.
Tiled Image Division
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
div_size = 256 | |
dst_file_ext = Path.extname(src_file) | |
dst_file_basename = Path.basename(src_file, dst_file_ext) | |
dst_files = | |
Stream.unfold(0, fn counter -> {counter, counter + 1} end) | |
|> Stream.map(& "#{dst_file_basename}_#{&1}#{dst_file_ext}") | |
div_img = | |
Evision.imread!(src_file) | |
|> Evision.Nx.to_nx() | |
|> Nx.to_batched_list(div_size) # divide image horizontally | |
|> Enum.map(& Nx.transpose(&1, axes: [1, 0, 2])) | |
|> Enum.flat_map(& Nx.to_batched_list(&1, div_size)) # divide images vertically | |
|> Enum.map(& Nx.transpose(&1, axes: [1, 0, 2])) | |
|> Enum.map(& Evision.Nx.to_mat!(&1)) | |
Enum.zip(div_img, dst_files) | |
|> Enum.map(fn {img, dst_file} -> Evision.imwrite!(dst_file, img) end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment