PixelValue
will extract the channel values at a certain pixel. It returns a vector of numbers, not a colour. Is there a simple way to extract the colour of a pixel as an expression that is ColorQ
? The image could have an arbitrary colour space.
1 Answer
$\begingroup$
$\endgroup$
4
Perhaps something like this:
ColorConvert[
PixelValue[im, pos],
OptionValue[Options[im], ColorSpace] -> ImageColorSpace[im]
]
where im
is an image and pos
is a pixel position, e.g., {100, 150}
.
The basic idea is to use ColorConvert
to automatically get the correct head for the image's colour space (e.g. RGBColor
or CMYKColor
). For this, we need the syntax ColorConvert[list, colspace1 -> colspace2]
where colspace1
should define the interpretation of the values in the list.
-
$\begingroup$ Could you explain the second argument? To me the left-hand side of the arrow seems like it should return the same as the right-hand side. What is the difference? $\endgroup$– C. E. ♦Commented May 6, 2020 at 15:09
-
1$\begingroup$ @C.E. Sorry, I should have been more verbose (I'm trying to get something working in parallel to asking...) What I am still confused about is the impact of the
ColorSpace
being aColorProfileData
object. I was trying to deal with this. For example, take the first example from theColorProfileData
doc page. The version I showed here effectively doesColorConvert[{a,b,c,d}, ColorProfileData[...] -> "CMYK"]
. I think this is the correct interpretation of the colour. We cannot have aColorProfileData
in a colour object (likeCMYKColor
), so on the RHS I just used"CMYK"
. $\endgroup$– SzabolcsCommented May 6, 2020 at 15:21 -
$\begingroup$ Any feedback about the use of
ColorProfileData
is welcome ... $\endgroup$– SzabolcsCommented May 6, 2020 at 15:21 -
$\begingroup$ @C.E. I am thoroughly confused. New question: mathematica.stackexchange.com/q/221271/12 $\endgroup$– SzabolcsCommented May 6, 2020 at 15:33