3
$\begingroup$

When you resize an image in a notebook with your mouse you get a tool tip with size and a magnification percent for your image:

enter image description here

Is there some way to programmatically ascertain these values in the tooltip? The values (both in the tooltip and the toolbar) seem to change along with the Magnification of the notebook. Note that ImageDimensions is not the solution here:

enter image description here

$\endgroup$
  • $\begingroup$ It's kinda boring. Go to the output cell containing your resized image and type in ImageDimensions @ before the image. Then evaluate as usual. $\endgroup$ – J. M. is away Apr 15 '16 at 0:20
  • $\begingroup$ No, that's not what I mean, I'm looking for the numbers in the tool tip. $\endgroup$ – M.R. Apr 15 '16 at 0:24
2
$\begingroup$

Here is another solution in the spirit of the answer by M.R. (should work starting from Mathematica version 7):

AbsoluteImageDimensions[img_Image] := 
  Module[{m = CurrentValue[Magnification], w0, h0, w}, 
    {w0, h0} = ImageDimensions[img];
    w = Options[img, ImageSize][[1, 2, 1]];
    {m {w, w*h0/w0}, 100 (m w/w0)^2}]

screenshot

And here is a better version which handles the most of practical cases with exception to the situation when the image wasn't resized by hand but was resized due to the ImageSizeMultipliers option (in this case it simply ignores the result of applying of this option; note that if the image was resized by hand this option does not matter) or using the internal Magnification option of the Image (which is always Automatic if the image was resized by hand, i.e. does not affect the result):

AbsoluteImageDimensions[img_Image] := 
 Module[{m = CurrentValue[Magnification], w0, h0, is, w, h},
  {w0, h0} = ImageDimensions[img];
  is = Options[img, ImageSize][[1, 2]];
  Switch[is,
   Automatic | {Automatic, Automatic}, {w, h} = {w0, h0},
   {_?NumericQ, _}, {w, h} = {is[[1]], is[[1]]*h0/w0},
   {_, _?NumericQ}, {w, h} = {is[[2]]*w0/h0, is[[2]]}
   ];
  {m {w, h}, 100 (m w/w0)^2}]

screenshot2

$\endgroup$
2
$\begingroup$

Here is one approach (Mathematica version 10 is required). Paste the following code as the next cell after the cell with your image and evaluate:

cell = NotebookRead[PreviousCell[]];
m = CurrentValue[Magnification]
Cases[cell, 
 GraphicsBox[__, 
   OrderlessPatternSequence[ImageSize -> {w_, _}, ___, 
    ImageSizeRaw -> {w0_, h0_}], ___] :> {m {w, w*h0/w0}, 100 (m w/w0)^2}, Infinity]
1.5

{{{273., 273.}, 331.24}}

screenshot

Note that the implementation of the image editing toolbar is contained in file

FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "SystemResources", 
  "AttachedImage2D.nb"}]
$\endgroup$
1
$\begingroup$

Using Alexey's pattern:

AbsoluteImageDimensions[img_] := 
 Module[{m = CurrentValue[Magnification]},
  Cases[{ToBoxes@img}, 
   GraphicsBox[___, 
     OrderlessPatternSequence[
      Verbatim[Rule][ImageSizeRaw, {w0_, h0_}], 
      Verbatim[Rule][PlotRange, _], 
      Verbatim[Rule][ImageSize, {w_, _}]]] :> {m {w, w*h0/w0}, 
     100 (m w/w0)^2}, \[Infinity]]
  ]

enter image description here

enter image description here

$\endgroup$
  • $\begingroup$ You don't need Verbatim in the pattern: it works reliably as written in my first answer. Also please see my new answer. $\endgroup$ – Alexey Popkov Apr 21 '16 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.