8
$\begingroup$

I use the export command

Export[path_to_png_image,content,ImageSize->{xres,yres},Background->None]

to create PNG images with transparency and some fixed dimension in pixels (1280x960). Strange enough, Finder (I am on macOS) shows me a "resolution" of 144 for the exported images. Indeed, I can add the Option ImageResolution->integer in the export command and if I do so, Finder afterwards shows me the chosen integer as the "resolution". This makes no sense to me as the images have the same dimension in pixels and have the same file size. In principle, I could just ignore this issue, but unfortunately it matters when I import the pictures into Keynote. If I import a 1280x960 pixel image with a "resolution" of 144 into Keynote, its "Original size" according to Keynote is 640x480.

Edit: So apparently PNG has an optional header with the DPI

https://www.w3.org/TR/2003/REC-PNG-20031110/#11pHYs

which is probably controlled with ImageResolution->integer setting. But what does Mathematica exactly do when I crank up ImageResolution from 72 (what I assume is the standard resolution)? Compute more subpixels and then average over the subpixels to create one pixel?

$\endgroup$
5
  • $\begingroup$ I am not sure that this is Mathematica's doing. On Windows, if I export a PNG at a certain ImageSize without setting an ImageResolution and then import it in GIMP, then GIMP reports a 72 dpi resolution (i.e. "screen resolution"). If, on the other hand, I specify an ImageResolution as well, then the same image resolution is reported in GIMP as well. Not sure where the problem is. In any case, please add details on your version etc (for instance, add the output of $Version to your post). $\endgroup$
    – MarcoB
    Jan 24, 2021 at 19:00
  • 2
    $\begingroup$ RasterSize controls the number of pixels in the resulting image. ImageResolution controls the size of said pixels. Any app that has to deal with scaling needs to know both values in order to accurately display the image on a device. $\endgroup$
    – ihojnicki
    Jan 24, 2021 at 20:15
  • 4
    $\begingroup$ I believe the original size in Keynote is given in points (1/72 inch). The 1280x960 dimension in pixels in Finder (in Tools>Adjust Size) with a resolution of 144 dpi corresponds to a width and height in points of 640x480 (just select points instead of pixels to verify) because 1 pixel is 1/144 inch. At a resolution of 72 dpi, 1 pixel is 1/72 inch, so points and pixels have the same magnitude. Resolution is important for display and printing but does not determine the dimensions of the image in pixels. $\endgroup$ Jan 24, 2021 at 20:29
  • 3
    $\begingroup$ I’m voting to close this question because this is not a Mathematica issue, but mainly a MacOS issue (and perhaps to some extent) a Keynote issue). $\endgroup$
    – m_goldberg
    Jan 25, 2021 at 15:16
  • $\begingroup$ @m_goldberg I have clarified the question. $\endgroup$
    – 220284
    Jan 27, 2021 at 12:54

1 Answer 1

11
+50
$\begingroup$

It is manipulating the transform between points and pixels. Nothing more, nothing less.

It might be more illustrative if you mess around with Rasterize specifying both ImageSize and RasterSize and using Information[img, "ImageResolution"] on the result. Export does have some legacy behaviors that can confuse matters.

For instance Rasterize[Graphics[Disk[]], ImageSize -> {300, 300}, RasterSize -> {20,20}].

In the case of importing a PNG file with dpi included in the metadata, that allows the FrontEnd to calculate what the native ImageSize for that particular image should be.

$\endgroup$
6
  • $\begingroup$ Thanks, that helpful! I never actually look at the image in mathematica itself (and if I understand things correctly, ImageSize controls how the picture is displayed in mathematica). So in this case it looks to me like I have full control over the PNG parameters by just using RasterSize and ImageResolution. Is this correct? $\endgroup$
    – 220284
    Jan 28, 2021 at 11:07
  • $\begingroup$ Yes, you can spec just those two values. $\endgroup$
    – ihojnicki
    Feb 18, 2021 at 14:29
  • $\begingroup$ "Export does have some legacy behaviors that can confuse matters." Do you mean this?: mathematica.stackexchange.com/q/101189/1871 $\endgroup$
    – xzczd
    May 7, 2021 at 2:26
  • 1
    $\begingroup$ @xzczd No. ImageResolution indicates the physical size of the pixels in the image. i.e., the number of pixels per inch. Spec'ing ImageResolution->500 and ImageSize->500 (** see comment below **) means that you are setting the image to be 1 inch wide. So in applications that care about such things (Word, Keynote, Mathematica, etc...), they will appear smaller. $\endgroup$
    – ihojnicki
    May 7, 2021 at 12:02
  • 2
    $\begingroup$ The confusing part is that Export historically used ImageSize as pixels. Which is not how it was documented, but happened to work because the renderer was not really resolution independent until 12.1. Most uses of ImageSize in Export should be using RasterSize instead. $\endgroup$
    – ihojnicki
    May 7, 2021 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.