3
$\begingroup$
h = ExampleData[{"TestImage", "House"}];
Grid[{{Show[h], 
   Show[h, Graphics[Line[{{0, 0}, ImageDimensions[h]*2}]], 
    PlotRange -> All]}}]

The second image is much smaller than the first. I want those two images to have same sizes.

Attempt 1

h=ExampleData[{"TestImage","House"}];
hDim=ImageDimensions[h];
a=Grid[{{Show[h,PlotRange->hDim],Show[h,Graphics[Line[{{0,0},hDim*2}]],PlotRange->All]}}]

enter image description here

Basically, it works. But the size of two images are still slightly different. Besides, it seems that I can not control the size any more, like use ImageSize.

$\endgroup$
  • $\begingroup$ remove PlotRange-> All and check $\endgroup$ – Ali Hashmi May 27 '17 at 13:29
  • $\begingroup$ But then I can not see the entire line. $\endgroup$ – Shen Zhou May 27 '17 at 13:39
  • $\begingroup$ Grid[{{Show[h, PlotRange -> ImageDimensions[h]], Show[h, Graphics[Line[{{0, 0}, ImageDimensions[h]*2}]], PlotRange -> All]}}] $\endgroup$ – Ali Hashmi May 27 '17 at 13:50
  • $\begingroup$ Thanks for your reply. But it still not perfect. And this problem looks like an easy one. But I still can not find a perfect solution after couples of hours struggling. $\endgroup$ – Shen Zhou May 27 '17 at 23:40
  • $\begingroup$ Grid[{{Show[h, PlotRange -> hDim], Show[h, Graphics[Line[{{0, 0}, hDim*2}]], PlotRangePadding -> 0, PlotRange -> All]}}]? $\endgroup$ – kglr May 27 '17 at 23:53
2
$\begingroup$

You can make use of the new in M8 ImageSize syntax to do this:

h = ExampleData[{"TestImage","House"}];
hDim = ImageDimensions[h];
a = Grid[
    {{
        Show[h, ImageSize->hDim->200],
        Show[h, Graphics[Line[{{0,0}, hDim*2}]], ImageSize->hDim->200,
            ImagePadding->0, PlotRangePadding->0, PlotRange->All
        ]
    }},
    Alignment->Bottom
]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks a lot. Sorry for not voting due to my low reputation. $\endgroup$ – Shen Zhou May 28 '17 at 0:11
1
$\begingroup$

We can take control of the image size with ImagePad to resize the images and with the ImageSize -> option of the Show command. Here is some code that illustrates the approach.

ClearAll["Global`*"]

h = ExampleData[{"TestImage", "House"}];
{wd, ht} = ImageDimensions[h];

imgLeft = ImagePad[h, {{0, 0}, {0, ht}}, White];
sizeLeft = ImageDimensions[imgLeft];

imgRight = ImagePad[h, {{0, wd}, {0, ht}}, White];
sizeRight = ImageDimensions[imgRight];

line = Graphics[{Line[{{0, 0}, ImageDimensions[imgRight]}]}];
m = 1/2;

Grid[{{
   Show[imgLeft, ImageSize -> m*sizeLeft],
   Show[{imgRight, line}, ImageSize -> m*sizeRight]}},
 Alignment -> {{Left, Left}, {Bottom}},
 Frame -> All]

Side-by-side images

We use the Frame -> All option initially to see the image spacing, borders, etc, and remove it later.

To create the images at the same scale, we first note that the diagonal line is the diagonal of an image that is twice as large as the original. Knowing that, we pad the original to be the same width and height as the line to obtain an image for the right panel. Padding does not distort the original. For the left image we pad the original to the same height as right image. The padding is only applied to the right and at the top of the original.

We can introduce a "magnification factor", $m$, to control the overall size of the images. The magnification is applied in the ImageSize -> option of the Show commands.

Note that the GraphicsGrid command is similar to Grid command but seems to introduce additional, unwanted padding.

$\endgroup$

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.