2
$\begingroup$

I want to manually tag images.

For example, we prepare a list of images like

imgList = Table[Rasterize[i, ImageSize -> 50], {i, {a, b, c}}]

it gives

Now I made a loop like this

tagAssoc = <||>;
Do[
  Print[imgList[[i]]];
  tagAssoc[i] = Input[],
  {i, 1, Length@imgList}];
tagAssoc

Basically, in each loop, it print a image, then popup an input window for me to input the description of the image. All tag information is saved in tagAssoc. After loop finished, it looks like below

The print order of images should be reversed for better view and tagging. But this is not my concern. Since I only need to see one image at a time, how to make this loop update the same cell with different images instead of print all images in different cell.

$\endgroup$
5
  • 1
    $\begingroup$ Apparently you need Dynamic, not Print. $\endgroup$ Commented Aug 4, 2022 at 5:01
  • 2
    $\begingroup$ ..or use PrintTemporary? $\endgroup$ Commented Aug 4, 2022 at 5:07
  • $\begingroup$ @MikeHoneychurch Do you mean replace Print with PrintTemporary? But that does not work, it still outputs 3 images $\endgroup$ Commented Aug 4, 2022 at 5:52
  • $\begingroup$ @AlexeyPopkov I feel stupid, but could you show me how to do it? : ) $\endgroup$ Commented Aug 4, 2022 at 5:53
  • $\begingroup$ Example: mathematica.stackexchange.com/questions/270215/… $\endgroup$ Commented Jan 2, 2023 at 16:48

1 Answer 1

3
$\begingroup$

One simple approach is to replace Print with Dynamic wrapped by PrintTemporary:

tagAssoc = <||>;
PrintTemporary[Dynamic[imgList[[i]]]];
Do[tagAssoc[i] = Input[], {i, Length@imgList, 1, -1}];
tagAssoc

... or even simpler, you can use Monitor instead as Lukas Lang suggests in the comments:

tagAssoc = <||>;
Monitor[Do[tagAssoc[i] = Input[], {i, Length@imgList, 1, -1}], imgList[[i]]]
tagAssoc
$\endgroup$
9
  • $\begingroup$ Thank you so much for solution. One thing I do not understand. I thought i is local variable in Do loop, how can it affect outside i? $\endgroup$ Commented Aug 4, 2022 at 6:50
  • $\begingroup$ @matheorem if you look at the documentation of Do (specifically the second to last point in "Details"), you'll see that it effectively uses Block, which implements dynamic scoping. As you can see in the documentation, this means that the global value of i is temporarily changed. $\endgroup$ Commented Aug 4, 2022 at 7:40
  • $\begingroup$ @matheorem It's an interesting question. As I understand, during evaluation of Do the scoped variable is changed globally, but after leaving the Do scope the variable is set up to the value it had before intering Do. But I can be slightly wrong in the details. It is worth to ask a separate question on it. $\endgroup$ Commented Aug 4, 2022 at 7:41
  • $\begingroup$ @AlexeyPopkov you could probably use Monitor instead of manually combining PrintTemporary and Dynamic $\endgroup$ Commented Aug 4, 2022 at 7:42
  • 1
    $\begingroup$ @AlexeyPopkov Oh, it seems that someone already explain it mathematica.stackexchange.com/a/83043/4742 Fred Simons mentioned when Table or Do finished, it does not inform the front end to update $\endgroup$ Commented Aug 4, 2022 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.