# Reproduce image effect in Mathematica

How do I create the "dotifying" effect below in Mathematica?

I have tried to use Rasterize first to get the image pixelated, but how do I get the disc/circle pattern then?

image := Import["image.jpg"]
Rasterize[image, RasterSize -> 150, ImageSize -> Full]


Original:

The result I want:

• Have you seen this? Feb 11, 2016 at 14:54
• Related: (8716) Feb 11, 2016 at 20:26
• @rhermans I will. Great answers! Feb 12, 2016 at 14:33

img = Import["https://i.stack.imgur.com/qzMGE.jpg"]


# ImagePartition and DominantColors

Make an array of Disk of the DominantColors in each part of ImagePartition.

Rotate[
Graphics@MapIndexed[
{First@DominantColors[#1, 1], Disk[#2, 1/2]} &
, ImagePartition[img, 10], {2} ]
, -π/2]


# ImageResize and ImageData

But I find the solution by @Szabolcs better, here I just do the rotation differently and add Background -> Black

Graphics[
MapIndexed[
{RGBColor[#1], Disk[{{0, 1}, {-1, 0}}.#2, 1/2]} &,
ImageData@ImageResize[img, {Automatic, 80}]
, {2}
], Background -> Black]


# Removing Moiré pattern

And yet another rotation option.

Export[
"Q106165.PDF",
Graphics[
MapIndexed[
{RGBColor[#1], , Disk[#2, 1/2]} &,
Transpose@
ImageData[ImageResize[img, {Automatic, 80}], DataReversed -> True]
, {2}
], Background -> Black]]


Here's my solution. Change CompilationTarget -> "C" to CompilationTarget -> "WVM" if you don't have a C compiler available.

cf = Compile[{{v, _Real}, {kernel, _Real, 2}},
v*kernel,
RuntimeAttributes -> {Listable},
Parallelization -> True,
CompilationTarget -> "C",
RuntimeOptions -> "Speed"
];

shapedPixels[img_, kernel_] := With[{dim = ImageDimensions[img]},
ImageCrop[
Image[Join @@
Transpose[
Join @@@
Transpose[
cf[ImageData[
ImageResize[img,
Ceiling[dim/Reverse[Dimensions[kernel]]]]], kernel], {1,
2, 5, 4, 3}], {1, 3, 2, 4}]], dim]];

Manipulate[
shapedPixels[pic,
1}, {matrix, {DiskMatrix, DiamondMatrix, BoxMatrix, IdentityMatrix,
CrossMatrix}}, {{invert, False}, {True, False}}]


Another approach:

pic = Import@"https://i.stack.imgur.com/qzMGE.jpg"

Image @ ArrayFlatten @ Map[
Map[Function[x, x #], DiskMatrix[5], {2}]&,
ImageData@ImageResize[pic, {Automatic, 50}],
{2}
]


I'm not taking care about preserving image size, it is governed by Resize and DiskMatrix size.

just put e.g. DiamondMatrix[5] or Rescale@GaussianMatrix[10] to get more fun:

You need to:

1. Rescale the image to a smaller size, ImageResize

2. Extract the pixel values, ImageData

3. Convert the triplets to RGBColor directive, and build a Graphics with appropriately coloured Disks inside. I found MapIndexed convenient for this.

Code:

img = ExampleData[{"TestImage", "Sailboat"}];

pixels = Transpose@ImageData[ImageResize[img, 50], DataReversed -> True];

g = Graphics[
MapIndexed[
{RGBColor @@ #1, Disk[#2, 1/2]} &,
pixels,
{2}
]
]


Update: when Mathematica renders graphics on screen, it rounds coordinates to screen pixels. This can induce ugly moire effects with repeating patterns like this. To avoid it, you can

Export to PDF and view the result:

Export["g.pdf", g]


This must be done using Export and not using the graphical interface.

Or rasterize at high resolution and downscale:

factor = 5;
ImageResize[Rasterize[g, "Image", ImageResolution -> 72 factor], Scaled[1/factor]]

• It seems some of the disks are overlapping when I use your code, this can also be seen in your picture. How to avoid this? Visually it divides the image into rectangles. Feb 11, 2016 at 14:54
• It looks a bit more like the photoshop version if you add Background -> Black into the Graphics command. Feb 11, 2016 at 15:13
• @macurie I cannot see this on my high-res screen but I think I know what you mean. Mathematica tends to round everything to screen pixels, which can induce the moire-like effect you describe. Export the graphics to PDF, and view it with a PDF viewer. Alternatively export it to a bitmap at very high resolutions and downscale it to a a more reasonable size. The effect should disappear. Feb 11, 2016 at 15:14
• @macurie One more note: do not export by right-click -> Save Graphics As... You must use the Export command to make sure the effect disappears. Feb 11, 2016 at 15:15