9

I have wondered more and more often lately how the size of an image is handled when displayed or exported. The documentation implies that the via ImageSize the size could be set in printer points (per inch). That does however not correspond with the displayed graphics (for instance ImageSize-> {72 1, 72 1 }) does not render a generic plot with the expected size of one inch square. (UI magnification at 100%)

I am using Mathematica 9 on OS X 10.9 and usually use .eps for exporting plots (since .pdf export doesn't really work that well with fonts and all)

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Sascha
  • 8,459
  • 2
  • 32
  • 66
  • This is Export-related only (but duplicate-ish): http://mathematica.stackexchange.com/q/5442/131. I guess sizes on-screen are another animal entirely. – Yves Klett Nov 19 '13 at 15:06
  • ImageSize only changes the display. To actually change the size of the image in pixels (for instance when you export), use ImageResize. Then you can specify exactly how big/small you want it. – bill s Nov 19 '13 at 15:29
  • 2
    For exporting to PDF, I sometimes need to use the workaround cm = 72/2.54; Export["file.pdf", Show[graphics, ImageSize -> 10 cm]]. This gives a size of precisely 10 cm. – Szabolcs Nov 19 '13 at 17:50
  • Take a look here, some of the ideas may be of use to you. – Szabolcs Nov 19 '13 at 20:48

1 Answers1

11

I export graphics like this:

cm = 72/2.54;
Export["file.pdf", Show[graphics, ImageSize -> 10 cm]]

For some reason Show is required when exporting to PDF, i.e. you can't just use the ImageSize option directly in Export and still get the expected result.


Regarding display on the screen, ImageSize is interpreted as pixels for on-screen display and as PostScript points (1/72 inch) for exporting to printable formats such as PDF. In other words, Mathematica always assumes the display to be a 72 ppi one.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 3
    N.B. ImageSize is (documented to be) printer's points whether onscreen or on paper. It just happens that many screens have (or used to have) 72 pixels per inch, and Mathematica uses this as a default instead of the actual resolution, as you noted in your answer in the link. Why it does this, I don't quite know. – Oleksandr R. Apr 20 '14 at 01:14
  • @OleksandrR. and Szabolcs, If I understand correctly that means for MMA pp == px is always True? And this is quite fundamental and not mentioned in documentation, right? p.s. There is m cm naming problem. – Kuba Feb 11 '16 at 14:00
  • @Kuba what does pp == px mean? – Oleksandr R. Feb 11 '16 at 14:02
  • @OleksandrR. printer's points, pixels. – Kuba Feb 11 '16 at 14:05
  • and by always I meant by default. :P – Kuba Feb 11 '16 at 14:05
  • @Kuba I don't know whether the default may change in future, but it does know what the actual screen resolution is even though it doesn't use it. See "ScreenInformation" /. SystemInformation["Devices"] vs. Options[$FrontEndSession, FontProperties] – Oleksandr R. Feb 11 '16 at 14:06
  • @OleksandrR. But not mentioning this in ImageSize documentation is a serious thing (for those who care about style a lot). P.s. I put SetOptions[EvaluationNotebook[], FontProperties -> {"ScreenResolution" -> 96}] and I expect Pane/@{ RandomImage[1, {100, 100}], RandomImage[1, {100, 100}, ImageSize -> {100, 100}]} to have different sizes in the screen, they don't am I missing something? – Kuba Feb 11 '16 at 14:11
  • 1
    @Kuba I agree that this doesn't seem right. Sometimes an assumption of 72 dpi is hard-coded and ImageSize uses that assumption; other times there is the ImageResolution option as well. It seems that ImageSize is always supposed to be printer's points, though, even if the front end doesn't know how many pixels that corresponds to. RasterSize is explicitly pixels. – Oleksandr R. Feb 11 '16 at 14:25
  • @Kuba When exporting graphics, it's true (with default settings) that pp = px. But I am not certain about exporting other notebook objects, such as Row, Column or Legended (!!!). I think Mma was using the "Printout" environment for notebook objects which has a smaller-than-one scaling factor by default. I don't remember the details, but I know Alexey Popkov had either a question or answer (or both?) on this topic (shrinkage with Printout environment). – Szabolcs Feb 11 '16 at 14:27
  • 1
    @Kuba The ImageResolution option can change this 72. It has to be used in Rasterize or Export. Another thing: do not use ImageSize in Export, use it instead as a Graphics option to get the desired result. Export[..., g, ImageSize -> ..., ImageResolution -> ...] is bad. Export[..., Show[g, ImageSize -> ...], ImageResolution -> ...] is good. At least for bitmap formats like PNG. But I don't understand why! – Szabolcs Feb 11 '16 at 14:29
  • @OleksandrR. That's ridiculous... Szabolcs, I see, well I'm more concerned about displaying in notebook than about exporting at the moment. – Kuba Feb 11 '16 at 14:30
  • @Kuba There's also an in-notebook ruler: http://mathematica.stackexchange.com/a/750/12 – Szabolcs Feb 11 '16 at 14:41