Replacing pixels with custom images in Mathematica?
How can I replace pixels of a binarized image with a custom image in Mathematica? I realized that once I have a matrix M of 0 or 1 entries depending on the white or black pixel (which I can get using Binarize and manipulating the output a bit), I can use Graphics [] to place a custom image with a square border in a grid where there is a 1 and a flat background when there is a 0, but I don't know exactly how to do this. Thank you in advance:)
source to share
If M
is your matrix containing 0 and 1 and image0/image1
is the images you want to display:
image0 = Graphics[{Red, Disk[]}, ImageSize -> 10];
image1 = Graphics[{Blue, Rectangle[]}, ImageSize -> 10];
M = {{0, 1, 0}, {1, 1, 1}, {1, 0, 0}};
You can simply do this:
GraphicsGrid[M /. {0 -> image0, 1 -> image1}]
or, if you want 0 to be empty:
GraphicsGrid[M /. {0 -> "", 1 -> image1}]
source to share
Here's one way:
mat = RandomInteger[1, {10, 10}];
Graphics[MapIndexed[If[#1 == 1, Disk, Circle][#2, 0.4] &, mat, {2}]]
I like to use different versions for this MapIndexed
. Instead of Disk
or, Circle
you can use any other graphic object. Just create a function that will take position as an argument and create this object.
source to share