0
$\begingroup$

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]]

Loading from imgur I get: enter image description here

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

Thanks!

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – ihojnicki
    Commented Dec 18, 2020 at 16:33

1 Answer 1

1
$\begingroup$

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.

loaded image

Here is the full-sized image.

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]]]

plot_1

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

plot_2

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

plot_3

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.

$\endgroup$
1
  • $\begingroup$ Mixing ImageDimensions and ImageSize is a recipe for... things breaking. One is in pixels, the other is in points. $\endgroup$
    – ihojnicki
    Commented Dec 18, 2020 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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