Message Boards Message Boards

0
|
1553 Views
|
8 Replies
|
1 Total Likes
View groups...
Share
Share this post:

A problem of ImageSize setting for an image to display in MM 13.3

Posted 5 months ago

I try to define a width for Graphics or Plot in Mathematica 13.3 in windows 11. But the result always seems to be not exactly, for example,

Graphics[Circle[], ImageSize -> 200]

ImageDimensions[%]

The width of the image is not 200 but 267, which is shown in the following figure. There is no question in MM 11.

enter image description here

POSTED BY: Jianhua Yang
8 Replies

On UHD 'retina' type displays (i.e., Macs, some Windows), which have roughly twice the resolution of HD, it is common for display objects to be rendered at double-resolution (i.e., double 'pixel size') so they end up about the same size on the screen (but of course, sharper). That is why for me and other posters the ImageDimensions of that graphic are {400,400}. Yours of {267,267} comes from the following calculation: 200*96/72, which tells me your screen has 96dpi.

POSTED BY: Gareth Russell

On my Mac I get an ImageDimensions of {400, 400}. It does not depend on the window magnification. It is funny that

ImageQ[Graphics[Circle[], ImageSize -> 200]]

gives False, but still ImageDimensions does not complain.

POSTED BY: Gianluca Gorni
Posted 5 months ago

Sorry, I know this is confusing, but I think you misunderstood Ian's comment or my comment. For any given image that you look at on a computer, there is some 2-dimensional representation of that image. The items in the "cells" of that 2D structure (I used "pixel" to refer to that before, but that was confusing because it has nothing to do with the pixels on your computer screen) are the color specifications. For monochromatic images each "cell" will contain a single number, but for color images each "cell" will contain a list of several numbers.

When you use ImageDimensions, you are asking about that 2D structure of "cells". This might have nothing at all to do with how big the image appears on your screen. ImageDimensions is purely about the numeric, 2-dimensional data underlying an image. That's why it doesn't depend on window magnification.

On the other hand, ImageSize does correlate to the visual size of the image on your screen. But this correlation is a bit convoluted. You brought up window magnification, for example, which is a transformation that you choose to apply, via your browser presumably, to an image after it's already been processed by the intervening software. So, very generally, if you are using "standard" presentation options, then ImageSize will correspond to printers points. But even then, I think there are nuances that make it difficult to programmatically determine an image's "true" size in the sense of what a viewer would measure with a ruler.

But I go back to my question about your use case. Is this just an academic question, or do you have a problem that you're trying to solve and ImageDimensions and ImageSize are not getting you to a solution? If you explain what you're trying to achieve, we might be able to show you how to do it.

POSTED BY: Eric Rimbey

“Printer’s points” are 1/72nd of an inch. The size of a pixel is device dependent. On Windows, a 1080p display is typically around 1/96th of an inch.

POSTED BY: Ian Hojnicki
Posted 5 months ago

Think of ImageDimensions as the dimensions in pixels. If you looked at the actual numerical data, it would be a matrix of size 267 x 267. Each cell of the matrix is one color datum.

ImageSize is more abstract than that. The documentation says it's "printer's points", but that is itself a bit mysterious, and I can't adequately explain it.

What sort of problem is this causing you? Do you need the actual pixel dimensions to be some specific value for some reason? What are you trying to achieve?

POSTED BY: Eric Rimbey
Posted 5 months ago

Thanks for your, lan's and Gialuca's comments. In my application I'd like to crop an image drawn by Plot or Graphics with ImageCrop[image, size, spec], where size is the size of the image by a fraction. For an example,

size={200,200};

img=Graphics[Circle[],ImageSize -> size];

ImageCrop[img,0.9size];

However, the size of img is not 200, but some other one, I can not get the right result. Yes, the third one code could be replaced by,

ImageCrop[img,0.9ImageDimensions[img]];

But I'm not sure if it is always true. There is no such problems in Mathematica 11.

POSTED BY: Jianhua Yang
Posted 5 months ago

Okay, let's walk through what's happening.

size = {200, 200};
img = Graphics[Circle[], ImageSize -> size]

You now have a Graphics expression. It gets displayed in the UI and looks like a circle. However, this is not an Image. The ImageSize options does NOT set the size of the matrix of image data. It's just telling the UI how large to display the image according to some nuanced reasoning that we've already discussed. So, if you want to end up with an image, an actual image data structure that has certain dimensions, this WILL NOT WORK.

ImageCrop[img, 0.9 size]

The image dimensions of the displayed Graphics expression is NOT {200,200}. Let's say it's {400,400}. Now you're cropping to {180,180}, which will crop out everything except the empty middle part.

So, you figured out a workaround:

ImageCrop[img, 0.9 ImageDimensions[img]]

And this works, but what would be much, much easier is to work with actual image data instead of graphics data. You can use Rasterize to turn just about anything into an image.

newImg = Rasterize[img, RasterSize -> {200, 200}]

Now you'll have an actual Image whose dimensions are {200,200}. In my Mathematica, it still DISPLAYS as the same size, but you can see the pixelization because of that. Use RasterSize -> {50, 50} to see this effect more clearly.

Okay, so now you're ready to crop. Let's just bundle it all together.

rasterSize = {200, 200};
graphicsSize = {500, 500};
cropFactor = .9;
ImageCrop[
 Rasterize[Graphics[Circle[], ImageSize -> graphicsSize], RasterSize -> rasterSize], 
 cropFactor rasterSize]

You can play with different values for rasterSize and graphicsSize so maximize or minimize pixelation.

POSTED BY: Eric Rimbey
Posted 5 months ago
POSTED BY: Jianhua Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract