Group Abstract Group Abstract

Message Boards Message Boards

0
|
3K Views
|
8 Replies
|
1 Total Like
View groups...
Share
Share this post:

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

Posted 1 year 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
POSTED BY: Gareth Russell

OK! Thank you very much for your detailed reply.

POSTED BY: Jianhua Yang
Posted 1 year 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

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 1 year ago
POSTED BY: Eric Rimbey

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

“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 1 year 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard