# How can I extract the data from an image and process it pixel by pixel?

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

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


but it just remains unevaluated:

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?

-
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

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

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


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


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

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


-
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?

guide/ImageProcessing

tutorial/ImageProcessing

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

-
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...

-
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}}


-