Group Abstract Group Abstract

Message Boards Message Boards

0
|
10.5K Views
|
11 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Graphics dimensions unexpected in 12.1

Posted 5 years ago

A code for data analysis in use in version 12.0 throws errors in version 12.1. I tracked the issue to version 12.1 generating graphics which appear to have an incorrect dimension . Below are two code segments with the same code executed in the two versions.

Version 12.0 produces a 2048x2048 image as expected:

$Version

(* "12.0.0 for Microsoft Windows (64-bit) (April 6, 2019)" *)

disk = Graphics[{Black, Disk[{985, 1030}, 600]}, 
   ImageSize -> {2048, 2048}];

ImageDimensions[disk]

(* {2048,2048} *)

For some reason, the same code in 12.1 produces a 3414x3414 image:

$Version

(* "12.1.0 for Microsoft Windows (64-bit) (March 14, 2020)" *)

disk = Graphics[{Black, Disk[{985, 1030}, 600]}, 
   ImageSize -> {2048, 2048}];

ImageDimensions[disk]

(* {3414,3414} *)

Does anyone have any insight into this? I have reported the issue to tech support and reverted to 12.0 for this work.

POSTED BY: David Keith
11 Replies
POSTED BY: Szabolcs Horvát
Posted 5 years ago
POSTED BY: David Keith
Posted 5 years ago

Thanks, Szabolcs. Makes perfect sense. And I agree about the automatic resolution. Things just work . . .until they don't.

POSTED BY: David Keith
POSTED BY: Szabolcs Horvát

I understand what you are doing, I am doing the same thing and adapting to this chance was annoying ...

You can set $ImageResolution = 72 at the beginning of the session.

Alternatively, you can make all rasterization explicit, and set ImageResolution -> 72. This is some trouble because I also used to do things like Binarize@Graphics[Disk[],...] instead of using Rasterize explicitly.

I have not refactored all my code for this, but I was thinking of using something similar to the following:

rast[im_?ImageQ][gr_Graphics] :=
 Module[{w, h},
  {w, h} = ImageDimensions[im];
  Rasterize[
   Style[
    Show[gr, PlotRange -> {{0, w}, {0, h}}, ImageSize -> {w, h}],
    Antialiasing -> False
    ],
   "Image",
   ImageResolution -> 72
   ]
  ]

Then if we have an image image = RandomImage[{0.9, 1}, {100, 100}], we can rasterize corresponding masks as mask = rast[image][Graphics[Disk[{50, 50}, 50]]].

I am curious how others handle this.

POSTED BY: Szabolcs Horvát
Posted 5 years ago

Dear @Szabolcs and others,

I really would like a simple method of using drawing graphics primitives within an image of fixed size, where the image size and the dimensions of the primitives can be given in pixels -- not printed size. It would be nice if there were a simple built in method for working in pixels.

But here is a method I think might work with the tool as it is:

Use the white rectangle to force the graphic to set the bounds for the graphic, then write objects within those bounds.

g = Graphics[{White, Rectangle[{1, 1}, {100, 100}], Black, 
    Disk[{50, 50}, 30]}, AspectRatio -> Automatic];

r = Rasterize[g, ImageResolution -> 72, ImageSize -> {100, 100}]

disk

In[3]:= r // ImageData // Dimensions

Out[3]= {100, 100, 3}

In[4]:= (* {100,100,3} *)
POSTED BY: David Keith
POSTED BY: Szabolcs Horvát

See my answer here:

https://mathematica.stackexchange.com/questions/219227/unexpected-behaviour-of-imagesize-and-imagedimensions-in-12-1/

Note that ImageDimensions works on Image and not on Graphics. When you apply it to graphics, they get implicitly Rasterized. 12.1 may rasterize at higher resolution, depending on the screen you have connected.

POSTED BY: Szabolcs Horvát
Posted 5 years ago
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard