# How can I get Inset to use ImageScaled, but preserve aspect ratio?

This code produces two identical images, despite the difference in the rectangles:

r1 = Rectangle[{-1, -1}, {1, 1}];
r2 = Rectangle[{-1, -1}, {0, 1}];
f[img_] :=
Show[ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}],
Graphics3D[{Inset[
Show[Graphics[img], ImageSize -> Large], {0.25, 0, 0}, Automatic,
ImageScaled[0.25]]}]]
f[r1]
f[r2]


If I do {Automatic,ImageScaled[0.25]}, I am told that it should be a pair of numbers, or a scaled or offset form.

I want the height of the inset to be 25% of the overall graphic, and the width to be scaled accordingly. I want it to work for any img, not just the two rectangles I gave; my actual use case involves .gif files. How do I make it so that f[r1] and f[r2] show different insets, i.e., inset the rectangles shown by Show[r1] and Show[r2], with the height scaled to 25% of the total height of the image?

-

I'm not exactly sure what the output should look like, but I think part of the problem lies in the way you have described Inset, from the documentation:

represents an object obj inset in a graphic.

Note that your code does not have a graphic into which your Inset is, well, inset.

If we redefine f:

f[img_] :=
Show[ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}],
Epilog -> {Inset[
Show[Graphics[img], ImageSize -> Large], {0.5, 0.5}, Automatic,
ImageScaled[0.25]]}]


We now get a plot that is at least responsive to the different shapes of r1 and r2

I needed to change your {0,0,0} to {0.5,0.5} because Epilog expects Graphics not Graphics3D and Inset is expected scaled coordinates, so the center of the image is {0.5,0.5}. ImageScaled[...] can be manipulated to change the relative size of the inset and I think what you're left with now is tweaking these parameters to get the image where you want on the plot.

-

See if this will work. It should, since you can get the ImageDimensions of an imported GIF. Note: I changed r1, r2 to Graphics. You can apply f to Graphics or to Image.

r1 = Graphics@Rectangle[{-1, -1}, {1, 1}];
r2 = Graphics@Rectangle[{-1, -1}, {0, 1}];
f[img_] :=
With[{g = ParametricPlot3D[{{t, 0, 0}, {0, t, 0}, {0, 0, t}}, {t, -1, 1}]},
Show[
g,
Graphics3D[{
Inset[img, {0.25, 0, 0}, Automatic,
ImageScaled[ 0.25 #/Last[#] &[ImageDimensions[img]/ImageDimensions[g]] ]
]}
]]
];
f[r1]
f[r2]


Caveat: ImageScaled depends on the dimensions of the enclosing image. They can change if the output of f is displayed with other things. For instance, I'm not sure how to handle {f[r1], f[r2]}. Perhaps one could scale by ImageDimensions@Rasterize[{f[r1], f[r2]}], but you'd have to know those dimensions before f was executed.

-