4
$\begingroup$

Is it somehow possible to recognize the color at each pixel in an image and show this color as a tooltip?

For example, with this picture:

cranial cross-section

I found a solution that uses LocatorPane in combination with DynamicModule:

DynamicModule[{pt = {1, 1}/2}, {LocatorPane[Dynamic[pt], img], 
  Dynamic[pt], Dynamic[RGBColor@PixelValue[img, {pt[[1]], pt[[2]]}]]}]

There is no Tooltip however. That I didn't figure out yet.

$\endgroup$
1

3 Answers 3

4
$\begingroup$

I have written this piece code (with the help of C.E.):

image = Import["https://i.sstatic.net/36JuK.png", "Image"];
Dynamic[
 pos = MousePosition["Graphics", {0, 0}];
 graphics = Graphics[{
    RGBColor[ImageValue[image, pos]],
    Rectangle[]
    }, ImageSize -> 100];
 Tooltip[image, graphics]
 ]

Demo

$\endgroup$
5
  • $\begingroup$ Somehow, that crashed Mathematica, when moving the mouse too fast. But code works, thank you! $\endgroup$
    – holistic
    Commented Aug 20, 2017 at 17:03
  • $\begingroup$ i can't fix that problem sorry,maybe someone else will improve the solution. you can also switch from mouseposition[], which force mathematica to re-evaluate everything each time the mouse move, to a different strategy ( for example retrieve the mouse position only when you click or double click with the mouse) $\endgroup$
    – Alucard
    Commented Aug 20, 2017 at 17:18
  • 2
    $\begingroup$ +1. It seems like it was displaying RGBColor that caused the crash. Displaying the color using Graphics appears to work. I made an update with that, I hope you don't mind. $\endgroup$
    – C. E.
    Commented Aug 20, 2017 at 19:42
  • $\begingroup$ @C.E. i appreciate it! i didn't know how to fix the problem $\endgroup$
    – Alucard
    Commented Aug 20, 2017 at 20:02
  • $\begingroup$ Thanks for thinking about it, works well! $\endgroup$
    – holistic
    Commented Aug 20, 2017 at 22:47
3
$\begingroup$

You can use built-in Image Assistant. Click once on an image in the notebook to select it. Image Assistant will appear under it:

enter image description here

In various tabs of the Image Assistant at the bottom you can find Tooltip Options. Select options you need and mouse-over pixels - you will get your tooltip as shown on the image below.

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you! But this does not work in my case, since I want to build it into a Manipulate later and it should be done automatically $\endgroup$
    – holistic
    Commented Aug 20, 2017 at 15:56
2
$\begingroup$

This is an extended comment on @VitalyKaurov's answer. That edit interface is actually loaded from here:

FrontEnd`FindFileOnPath["AttachedImage2D.nb", 
  "PrivatePathsSystemResources"] // FrontEndExecute

So you can look at the source for that to see how it's implemented in the front end (the code is actually pretty cool). You can also get the thing to wrap on a selected image box like so:

FEPrivate`WrapImageWithEditCell[InputNotebook[]] // FE`Evaluate;

Here's an example of that:

SelectionMove[EvaluationCell[], Before, Cell];
NotebookWrite[
  InputNotebook[],
  Cell[
   BoxData@ToBoxes@Import["https://i.sstatic.net/36JuK.png"],
   "Output"
   ]
  ];
box =
  Nest[
   First@*FrontEndExecute@*FrontEnd`ObjectChildren,
   PreviousCell[],
   1
   ];
SelectionMove[box, All, Box];
FEPrivate`WrapImageWithEditCell[InputNotebook[]] // FE`Evaluate;

Theoretically, then we could find a way to reuse what the FE uses to get a (mostly) pure front-end approach to this. Whether this is better than a Manipulate is up to preference.

$\endgroup$

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.