# Strange behavior of Show with picture + points

I have a square image of 1000 x 1000 pixels, and I want to generate a collection of points inside the image. Knowing that the image is a square, I thought of simply generating points in the domain {0,1000}, then using Show[] to observe the points (plus other computations later on, which is why I need this first). But when I use Show[] the image appears smaller, strangely always in the {0,750} range. Strangely enough (to me at least) when I load my image directly from here (imgur.com), there's no problem. Only when I load the image from my local disk, the image shrinks. Why would that make a difference? Any help appreciated, thanks! (I'm using MMA 12.1 on Windows 10).

nPts = 1000;
mySquaredImage = Import["https://i.sstatic.net/owGLA.jpg"];
myImageDimensions = ImageDimensions[mySquaredImage];
myPoints = RandomReal[{0, myImageDimensions[[2]]}, {nPts, 2}];
Show[ListPlot[myPoints, PlotStyle -> Black],
SetAlphaChannel[mySquaredImage, 0.2]]


Exactly the same code, loading image from local disk, I get:

Thanks!

• I'm betting that if you ran Information[localImage, "ImageResolution"] it would return something like 120 or 192. The one you download will return Automatic, which means 72. When it returns Automatic or 72, pixels and points are identical. Which is why your example happens to work on the web image. You need to convert coordinate systems for this to consistently work. Commented Dec 18, 2020 at 16:33

I couldn't reproduce your problem, most likely because I don't have access to the image on your local drive. I tried a few different variations with an JPG image from my own drive.

I didn't use Import to load the image, I just dragged it into my notebook from a file system window.

Here is the full-sized image.

Next I evaluated your parameters.

nPts = 1000;
SeedRandom[42];
myPoints = RandomReal[{0, myImageDimensions[[2]]}, {nPts, 2}];
myImageDimensions = ImageDimensions[craters]

{400, 400}


Finally, here are the graphic displays I made.

Show[
ListPlot[myPoints, PlotStyle -> Black],
SetAlphaChannel[craters, 0.6],
ImageSize -> myImageDimensions[[2]]]


Show[
SetAlphaChannel[craters, 0.6],
ListPlot[myPoints, PlotStyle -> Black],
AspectRatio -> Automatic, ImageSize -> myImageDimensions, Frame -> True]


Show[
SetAlphaChannel[craters, 0.6],
Graphics[Point[myPoints], PlotRangePadding -> None],
ImageSize -> myImageDimensions, Frame -> True]


I prefer the 3rd plot, but main point I want to make is that order in which the graphic elements are given to Show and where the graphics options are placed can make a big difference. I suggest you very careful to make sure when get an image from different sources you make sure the two Show expressions you use are exactly the same except for argument specifying the imported graphic.

• Mixing ImageDimensions and ImageSize is a recipe for... things breaking. One is in pixels, the other is in points. Commented Dec 18, 2020 at 16:23