Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, to change the color of each pixel to the mean color of the three channels, I tried

i = ExampleData[{"TestImage", "Lena"}];

but it just remains unevaluated:

enter image description here

How can I read the colors of an image into a list or matrix and change the color codes and save it back to an image?

share|improve this question
I would rather not close but on second read I agree that this question needs to be refined. – Mr.Wizard Feb 11 '12 at 14:58
I reworded the question. – nilo de roock Feb 11 '12 at 15:06
The title of your question was too broad, which attracted answers that were tangential. I think your core question was on how to access the underlying information pixel by pixel in an image. I've reworded your question to reflect that. Please let me know if this was indeed what you intended – R. M. Feb 11 '12 at 18:05
up vote 7 down vote accepted

Certainly. For instance, here's how to reduce the number of colours to 10 (randomly chosen in RGB space):

i = Import["ExampleData/lena.tif"]

Mathematica graphics

You can try ImageData[i] to see the actual RGB values for each pixel. Now produce ten random triplets of reals between 0. and 1., and construct a function to quickly pick the one closest to some given number:

colours = RandomReal[{0, 1}, {10, 3}];
nf = Nearest[colours];

Then map the thing over the RGB values of the image and look at it:

Map[First[nf[#]] &, ImageData[i], {-2}] // Image

Mathematica graphics

Try increasing the number of randomly selected colours to see what happens:

 Module[{colours = RandomReal[{0, 1}, {num, 3}], nf},
  nf = Nearest[colours];
  Map[First[nf[#]] &, ImageData[i], {-2}] // Image
 {{num, 10}, 1, 1000, 1}

Mathematica graphics

share|improve this answer
Note that good ol' Lenna can also be invoked with ExampleData[{"TestImage", "Lena"}]. – J. M. Feb 11 '12 at 15:09
Wow, and so easy. Thanks. – nilo de roock Feb 11 '12 at 15:09
@ndroock1 yes it feels like cheating – acl Feb 11 '12 at 15:11

Have you looked through the included documentation?



There are also a number of entries on the Wolfram Blog that relate to image processing:

the incredible convenience of mathematica image processing

aMAZEing image processing in mathematica

the battle of the marlborough maze at blenheim palace continues

fun with line-art

fixing bad astrophotography

To learn more on the subject ask Heike. ;-)

share|improve this answer
That is, I checked the Documentation Center. Thanks I'll plan a day to go through it all. – nilo de roock Feb 11 '12 at 15:02

The ImageApply function applies any suitable Mathematica function to every pixel in an image. You just have to specify the transformation you want to make. I recently asked this: Image levels: how to alter 'exposure' of dark and light areas? question - and got many good answers...

share|improve this answer
If I wasn't sitting here with just my iPad today I'd have given a better example... – cormullion Feb 11 '12 at 16:18

There are also several built-in effects available through ImageEffect. For example:

ImageEffect[ExampleData[{"Image","Lena"}], #]& /@ 
    {"Charcoal", {"OilPainting", 10}, {"Posterization", 5}}

Mathematica graphics

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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