2
$\begingroup$

Is it possible to get (not change!) ImageResolution for an existing Image, say the one I have imported from a file?

ImageResolution does affect Image's interaction with Graphics (it seems undocumented that Show works with Images but it's very convenient):

Module[{img, dims},
 img = ExampleData[{"TestImage", "F16"}];
 
 dims = ImageDimensions@img;
 GraphicsGrid@List@Table[Show[
     Image[img, ImageResolution -> res],
     Graphics[{
       Arrowheads[{-.05, .05}], Arrow[{{0, 0}, dims}],
       PointSize[Large], Point[dims/2.]
       }],
     Frame -> True,
     PlotLabel -> ImageDimensions[img]
     ]
    , {res, {$ImageResolution, 120}}
    ]
 ]

different image resolutions in Show

$\endgroup$
3
  • 1
    $\begingroup$ I find it easier to think in terms of image size in pixels when dealing with digital images on screen. To me, resolution must be associated with a print size to make sense (eg 300 dpi at 4x6" print size, or similar). Perhaps you could expand on what you want to achieve and how the resolution of the image would help you get there. $\endgroup$ Commented Feb 23, 2022 at 11:43
  • $\begingroup$ The idea is to make the image a background for the graphics. I already found the workaround — reset the image resolution to $ImageResolution every time when mixing with Graphics. But the question is about accessing this data of the image. Surely, Show does access it somehow. $\endgroup$ Commented Feb 23, 2022 at 12:44
  • $\begingroup$ Strongly related: Wrong Graphics coordinate system after Image to Graphics conversion. $\endgroup$ Commented Feb 24, 2022 at 2:11

2 Answers 2

6
$\begingroup$

Sure.

resolveImageResolution[img_Image] := Replace[Information[img, "ImageResolution"], Automatic -> 72]

That can return either a single value or a horizontal/vertical pair.

$\endgroup$
3
  • 1
    $\begingroup$ Is there a possibility that the value for Automatic will change in the future? $\endgroup$ Commented Feb 23, 2022 at 17:16
  • 1
    $\begingroup$ As @AlexeyPopkov said, the magic number 72 doesn't look very promising (considering $ImageResolution is Automatic. Otherwise thanks for the answer! $\endgroup$ Commented Feb 23, 2022 at 17:19
  • 3
    $\begingroup$ $ImageResolution is used as the default value for Rasterize. The FrontEnd is the one that actually resolves it to a value. I doubt I will ever change that, since it has backward compatibility implications. $\endgroup$ Commented Feb 23, 2022 at 19:08
2
$\begingroup$

(An addition to the answer by ihojnicki.)

The idea is to make the image a background for the graphics.

HighlightImage is a built-in implementation of this idea:

img = ExampleData[{"TestImage", "F16"}];
dims = ImageDimensions@img;
Table[HighlightImage[Image[img, ImageResolution -> res], 
  Graphics[{Black, Arrowheads[{-.05, .05}], Arrow[{{0, 0}, dims}], PointSize[Large], 
    Point[dims/2.]}], Frame -> True, 
  PlotLabel -> ImageDimensions[img]], {res, {$ImageResolution, 120}}]

screenshot

Strongly related:

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.