2
$\begingroup$

I use Mathematica version 12.2, Windows 10.

Graphics[{Red, Rectangle[{0, 0}, {200, 300}]}, ImageSize -> 200] // ImageDimensions
{200, 296}

I think the output shoud be {200, 300}, not {200,296}.
I added ImageMargins -> {{0, 0}, {0, 0}} options, but the result was the same.

Graphics[{Red, Rectangle[{0, 0}, {200, 300}]}, ImageSize -> 200, 
  ImageMargins -> {{0, 0}, {0, 0}}] // ImageDimensions
{200, 296}

Similary,

for {200, 300} I get {200, 296}
for {200, 200} I get {200, 200}
for {200, 100}, I get {200, 104}
for {200, 10}, I get {200, 26}
for {200, 1}, I get {200, 26}

I don't see any reasonable level of rules that are satisfactory. And how can I create x times y pixeled rectangle ? (if it is finally exported)

Also I Add a code to accurately represent the state of my Mathematica :

SystemInformation["Devices", "ConnectedDisplays"]

{{"Region" -> {{0., 1152.}, {0., 615.6}}, 
  "FullRegion" -> {{0., 1152.}, {0., 648.}}, 
  "PixelDimensions" -> {1920, 1080}, "BitDepth" -> 32, 
  "Resolution" -> 120., "Scale" -> 1.66667}}

SetOptions[$FrontEnd, 
 GraphicsBoxOptions -> {BaseStyle -> Magnification -> 3/5}];

(* I applied this option this time *)

$\endgroup$

1 Answer 1

3
$\begingroup$

For obtaining the true image size in printer's points, one should wrap the expression with Annotation and use Rasterize[..., "Regions"] to get its bounding rectangle:

Clear[expr, imageSize]
expr[h_, opts : OptionsPattern[]] := 
  Graphics[{Red, Rectangle[{0, 0}, {200, h}]}, ImageSize -> 200, opts];
imageSize[expr_] := 
 Differences[Rasterize[Annotation[expr, "out", "Region"], "Regions"][[1, 2]]][[1]]

imageSize[expr[300]]
{200., 295.68}

(It is possible to use ResourceFunction["GraphicsInformation"] or ResourceFunction["CellBoundingRectangle"] for obtaining the same information.)

As you can see, the vertical size is slightly different than expected. I believe this is a bug in Graphics rendering. To get the expected size, you need to specify the option PlotRangePadding -> None for Graphics:

imageSize[expr[300, PlotRangePadding -> None]]
{200., 300.}

With the other possible choices for the height you also get the expected result:

Table[imageSize[expr[h, PlotRangePadding -> None]], {h, {300, 200, 100, 10, 1}}]
{{200., 300.}, {200., 200.}, {200., 100.}, {200., 10.}, {200., 1.}}

You can check what image size you obtain on exporting to a vector graphics format:

vectorExportImportPDF[expr_] := 
 First@ImportString[ExportString[expr, "PDF", "AllowRasterization" -> False], 
   If[$VersionNumber >= 12.2, {"PDF", "PageGraphics"}, {"PDF", "Pages"}], 
   "TextOutlines" -> False]

vectorExportImportPDF[expr[300, PlotRangePadding -> None]] // Options
{ImageSize -> {200., 300.}, PlotRange -> {{0., 200.}, {0., 300.}}, AspectRatio -> Automatic}

The final rendered image size in pixels depends on the resolution of your monitor and local Magnification setting of a Notebook where the graphics is rendered. The pixel size should be equal to the image size in printer's poins times magnification*resolution/72., where resolution of your monitor can be obtained from SystemInformation:

resolution = "Resolution" /. First[SystemInformation["Devices", "ScreenInformation"]]

or from CurrentValue["WindowResolution"]:

resolution = CurrentValue["WindowResolution"]

However, note that prior to Mathematica 13.0, by default on Windows the monitor resolution was always assumed to be 72.. Since you're using version 12.2 on Windows and SystemInformation reports a monitor resolution of 120., you've probably disabled the compatibility mode via the option "ScreenResolutionCompatibilityMode". Starting from version 13.0 the compatibility mode by default is off, and SystemInformation reports the true resolution of the monitor (taking into account the scaling set at the OS level).

The local Magnification option by default is inherited from Notebook's Magnification:

magnification = CurrentValue[EvaluationNotebook[], Magnification]

But in your case for Graphics (and Image without explicit Magnification option) it will be equal to 3/5 because you have set a fixed Magnification for GraphicsBox via global GraphicsBoxOptions:

SetOptions[$FrontEnd, GraphicsBoxOptions -> {BaseStyle -> Magnification -> 3/5}];

Related MMa.SE threads:

Related WC threads:

$\endgroup$
2
  • $\begingroup$ Thank you very much, I think it is still not easy to create an image with small width or small height. I have an idea, a kind of workaround, but haven't tried it yet : embed the graphic that I want onto a slightly larger graphic(over 26 pixels), then export to image, then crop the image. Do you think it is a good idea..? $\endgroup$
    – imida k
    Commented Aug 22, 2022 at 2:55
  • $\begingroup$ @imidak It is not difficult. Just use Rasterize with the option RasterSize. For example, Rasterize[Graphics[Disk[{0, 0}]], RasterSize -> 15]. $\endgroup$ Commented Aug 22, 2022 at 21:38

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.