As I understand, you have these issues with version 13.x on Windows. The reasons for such behavior were previuosly discussed in the following questions:
Specifically for the image rendering quality I propose the following workaround which doesn't affect other parts of the system:
With[{resolution =
"Resolution" /. First[SystemInformation["Devices", "ScreenInformation"]]},
SetOptions[$FrontEnd,
GraphicsBoxOptions -> {BaseStyle -> Magnification -> 72/resolution}]]
Note however, that rendering of the image is also affected by the ImageResolution
setting, which in the case of your image is set to {100, 100}
:
Options[im, ImageResolution]
{ImageResolution -> {100, 100}}
By default, when the resolution isn't specified in an image itself, it is set to Automatic
, what is equivalent to 72.
dpi. independently of the true resolution of your monitor. The above workaround is developed for this case, since the situation when the resolution setting is absent is common. Hence with the above workaround you should also set ImageResolution -> Automatic
for your image:
Image[im, ImageResolution -> Automatic]
The pixel-perfect rendering means that each pixel of the image corresponds to exactly one screen's pixel. Let us compute pixel size of the rendered image and compare it to the true pixel size:
screenshot = Import["https://i.sstatic.net/hL5Hm.png"];
crop = ImageTake[screenshot, {419, 847}, {94, 592}];
ImageDimensions@ImagePad[crop, -BorderDimensions[crop, 0]]
ImageDimensions[im]
{486, 416}
{487, 416}
As you see, the rendering is still off by 1 pixel. This is really surprising, because ImageSizeRaw
of the GraphicsBox
produced in the Notebook is set to {487., 416.}
:
Options[ToBoxes@Image[im, ImageResolution -> Automatic], ImageSizeRaw]
{ImageSizeRaw -> {487., 416.}}
Apparently, this is another bug of the FrontEnd rendering algorithm.
Alternatively, without changing the default settign for GraphicsBoxOptions
, you could set ImageResolution
equal to the true resolution of your monitor:
With[{resolution =
"Resolution" /. First[SystemInformation["Devices", "ScreenInformation"]]},
Image[im, ImageResolution -> resolution]]
screenshot2 = Import["https://i.sstatic.net/DWJzA.png"];
crop2 = ImageTake[screenshot2, {348, 777}, {94, 593}];
ImageDimensions@ImagePad[crop2, -BorderDimensions[crop2, 0]]
{486, 415}
As you see, in this case the situation is even worse: the rendered image size differs from what is expected not only in the horizontal, but also in the vertical direction.
Response to the discussion in comments
@AlexeyPopkov, in a dpi scaling (aka device independent) world, pixel perfect is fleeting. It might be spot on for you on one machine, but wrong on another. Maybe that is good enough for a single user, but that would problematic when exchanging notebooks. – ihojnicki
We can easily see that Chrome by default always renders the image with pixel-pefect quality, so this argument is not convincing:
chromeImage = WebImage["https://i.sstatic.net/zGk92.png"]
ImageDimensions@ImageCrop@chromeImage
{487, 416}
The same is true for the other browsers as well as small applications like MS Paint and IrfanView. Why then such a giant application as Mathematica can't do this?
Response to the discussion in comments #2
@AlexeyPopkov, so the native ImageSize
for a bitmap is calculated using the following: N[ImageDimensions@img/Information[img, "ImageResolution"] * 72]
. When the RasterBox
is drawn, the viewbox is converted back to pixels using N[imagesize /72 * CurrentValue["WindowResolution"]]
. If you want a 1:1 transform, Information[img, "ImageResolution"]
must match CurrentValue["WindowResolution"]
OR you manually specify an ImageSize
. i.e., Image[im, ImageSize -> N[ImageDimensions@im/CurrentValue["WindowResolution"]* 72]]
. –
ihojnicki
@AlexeyPopkov, the difference between Chrome and Mathematica? Units. Mathematica, everything is in points (1/72nd of an inch). Paint uses pixels. Chrome uses a device independent pixel known as an angular measurement. AM is effectively 1/96th of an inch on a typical computer display. – ihojnicki
@AlexeyPopkov, probably because it was snapped to the pixel grid. The document's coordinate space uses real numbers, not whole integers. There is no user control over that. Could be a clipping bug (I know there are some on 96 dpi displays on Windows). – ihojnicki
Indeed, the check reveals that the rendered image is simply cut off on the right and bottom by 1 pixel. Implementing your second approach:
CellPrint[TextCell[
Image[im, ImageSize -> N[ImageDimensions@im/CurrentValue["WindowResolution"]*72]],
"Output", Background -> Black]]
screenshot3 = Import["https://i.sstatic.net/6tXlr.png"];
rendered = ImageCrop@ImageTake[screenshot3, {312, 735}, {92, 591}];
ImageDimensions@rendered
{486, 415}
Max@Abs[ImageData[RemoveAlphaChannel[im]][[;; -2, ;; -2]] -
ImageData[rendered]]
0.
So it is a clipping bug.