# How can I return the color at a coordinate in an image?

I have an imported and cropped image:

TestPicture = Import["filepath.jpg"];
TestPictureCropped = ImageCrop[TestPicture, Min[ImageDimensions[TestPicture]]];


How can I specify a coordinate in the image and return the color of the nearest pixel to the coordinate? Thanks for any help!

-

For example:

tp = ExampleData[{"TestImage", "Airplane"}];
Manipulate[Column@{tp, Extract[ImageData[tp], Round /@ p]},
{{p, {1, 1}}, Locator}]


-
ImageValue is also another choice – HyperGroups May 8 '13 at 4:33
This answer is incorrect because it messes up different coordinate systems, as can be seen with tp = Image@IdentityMatrix[100]. PixelValue and ImageValue are the preferred way to accomplish this task. – Matthias Odisio May 8 '13 at 17:50
This gives the wrong values! The values in p are coordinates, so they are 0-based, start at the bottom-left corner and are stored in x/y order. Extract expects array indices, which are 1-based, start at the top-left corner and are in row/column (i.e. y/x) order. So the values you get are transposed, upside down and shifted by one pixel. – nikie May 9 '13 at 9:16
Way to go beli :D – Rojo May 12 '13 at 4:41
tp = ExampleData[{"TestImage", "Lena"}];
Manipulate[Column @ {tp, ImageValue[tp, p]}, {{p, {1, 1}}, Locator}]


-

As a side-note - you can quickly get color info from images using image assistant. Just click once on the image and in version 9 assistant will appear below:

-

Also worth noting is that there's a companion function to ImageValue, called PixelValue. The difference between the two is, as far as I can tell from the documentation, that ImageValue interpolates the color of a coordinate according to the colors of nearby pixels, whereas PixelValue finds the color of the pixel whose centre is nearest to the integer values of the coordinates. The difference is hard to spot on smooth photographs, but possible:

tp = ExampleData[{"TestImage", "Mandrill"}];
Manipulate[Column
[{tp,
Text[p],
Text["ImageValue"],
ImageValue[tp, p],
Graphics[{
RGBColor[ImageValue[tp, p]],
Rectangle[{0, 0}, {3, 1}]}],
Text["PixelValue"],
PixelValue[tp, p],
Graphics[{
RGBColor[PixelValue[tp, p]],
Rectangle[{0, 0}, {3, 1}]}]
}],
{{p, {1, 1}}, Locator}
]


As you hinted, in addition to ImageValue interpolating neighboring pixel values, the coordinate systems of ImageValue an PixelValue are off by one half. See the 'Properties and Relations' section of reference.wolfram.com/mathematica/ref/PixelValue.html – Matthias Odisio May 8 '13 at 17:57