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 image

The result I want:

"Dotify" effect

  • 4
    $\begingroup$ Have you seen this? $\endgroup$ Feb 11, 2016 at 14:54
  • $\begingroup$ Related: (8716) $\endgroup$
    – Mr.Wizard
    Feb 11, 2016 at 20:26
  • $\begingroup$ @rhermans I will. Great answers! $\endgroup$
    – macurie
    Feb 12, 2016 at 14:33

4 Answers 4


Load image

img = Import["https://i.sstatic.net/qzMGE.jpg"]

ImagePartition and DominantColors

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

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

enter image description here

ImageResize and ImageData

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

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

Mathematica graphics

Removing Moiré pattern

And yet another rotation option.

   {RGBColor[#1], , Disk[#2, 1/2]} &, 
    ImageData[ImageResize[img, {Automatic, 80}], DataReversed -> True]
   , {2}
   ], Background -> Black]]

enter image description here


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}},
   RuntimeAttributes -> {Listable},
   Parallelization -> True,
   CompilationTarget -> "C",
   RuntimeOptions -> "Speed"

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

  ArrayPad[If[invert, 1 - matrix[r], matrix[r]], padding]], {r, 1, 20,
   1}, {padding, 0, 10, 
  1}, {matrix, {DiskMatrix, DiamondMatrix, BoxMatrix, IdentityMatrix, 
   CrossMatrix}}, {{invert, False}, {True, False}}]


Another approach:

pic = Import@"https://i.sstatic.net/qzMGE.jpg"

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

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

enter image description here

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

enter image description here enter image description here


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.


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

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

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

enter image description here

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]]
  • $\begingroup$ 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. $\endgroup$
    – macurie
    Feb 11, 2016 at 14:54
  • 1
    $\begingroup$ It looks a bit more like the photoshop version if you add Background -> Black into the Graphics command. $\endgroup$
    – bill s
    Feb 11, 2016 at 15:13
  • 2
    $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Feb 11, 2016 at 15:14
  • 2
    $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Feb 11, 2016 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.