4

Here's a way to reproduce the issue: export the following picture

Export["a.png", Plot[Sin@x, {x, -5, 5}], ImageSize -> 500, ImageResolution -> 500]
SystemOpen@Directory[]

and paste it into a blank .doc/docx file (either with Ctrl+c, Ctrl+v or the Insert -> Picture in the menu), then you'll see something like

enter image description here

Further check shows the picture isn't lossy compressed, it is just zoomed out to 1 inch wide. Why this happens? Is it a expected behavior?

I'm on v9.0.1, MS Office 2016 64bit, Win10 64bit.

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • Works as expected on "10.0 for Mac OS X x86 (64-bit) (December 4, 2014)" and MSW for Mac 14.5.8 I understand this behavior is as designed. You can grab a corner and dragging with the mouse the size. –  Dec 04 '15 at 13:54
  • I'm voting to close this question as off-topic because the question does not concern the technical computing software Mathematica, but is about the office software MS Word – Jason B. Dec 04 '15 at 13:57
  • @JasonB Well, I don't think so. This strange behavior won't appear if you casually find a picture somewhere and paste it into Word. – xzczd Dec 04 '15 at 14:00
  • @Lou That would be quite annoying if the size of the image needs to be accurate. – xzczd Dec 04 '15 at 14:02
  • @xzczd - I see your point. When I import a png I created using the Shutter screenshot program, that is 479 pixels wide and 480 pixels in height, it takes up almost a full page. When I import a picture that I exported in Mathematica, which is 1000 pixels wide and 617 pixels high, it is much smaller. I wonder what is different about the images. – Jason B. Dec 04 '15 at 14:07
  • But if you do need the size to be accurate, you can right click on the image and select "Image Size" you can set it manually – Jason B. Dec 04 '15 at 14:09
  • Maybe it's better if you look Which Stack Exchange is proper for asking Microsoft Word related questions? and ask at http://superuser.com/ –  Dec 04 '15 at 14:12
  • I think I can rephrase this as a question that has nothing to do with Word, if you don't mind, @xzczd - it's really to do with how ImageResolution and ImageSize play with each other. – Jason B. Dec 04 '15 at 14:20
  • @JasonB Feel free to edit :) – xzczd Dec 04 '15 at 14:22
  • This can probably be marked as a duplicate of this one: http://mathematica.stackexchange.com/questions/5442/real-size-image-printing – Jason B. Dec 04 '15 at 15:04

1 Answers1

5

The problem here lies in the fact that you have explicitly set the ImageSize variable. When you say ImageSize->1000 you are telling it that you want an image that is 1000 pixels wide. So when you set the resolution, which is measure in pixels per unit distance, to a higher value you are in effect setting the dimensions of the output image smaller.

Take these two outputted files,

Export["a500.png", Plot[Sin@x, {x, -5, 5}], ImageSize -> 1000, 
 ImageResolution -> 500]
Export["a72.png", Plot[Sin@x, {x, -5, 5}], ImageSize -> 1000, 
 ImageResolution -> 72]

On my system "a500.png" is 42.9 kB and "a72.png" is 32.6 kB, but both are around 1000x617 pixels. enter image description here Actually, "a72.png" is 1000x614 pixels, but that doesn't seem enough to account for the different file sizes - so if anyone knows what the extra data is doing, I'd be happy to find out.

Now take the same code, but this time don't explicitly set the ImageSize

Export["b500.png", Plot[Sin@x, {x, -5, 5}], ImageResolution -> 500]
Export["b72.png", Plot[Sin@x, {x, -5, 5}], ImageResolution -> 72]

Now the file "b72.png" is 15.6 kB and "b500.png" is 141.2 kB and has many more pixels,

enter image description here

Here are the images imported into word without any resizing, on the left page is a500 followed by a72, and on the right side is b500 followed by b72.

enter image description here

Edit: Taking from this answer, the following will allow you to export two images, one looking a lot better than the other because it has a higher resolution, but both will have the same dimensions,

Export["c500.png", 
 Plot[Sin@x, {x, -5, 5}, 
  ImageSize -> {72 QuantityMagnitude[Quantity[4, "Inches"]], 
    Automatic}], ImageResolution -> 500]
Export["c72.png", 
 Plot[Sin@x, {x, -5, 5}, 
  ImageSize -> {72 QuantityMagnitude[Quantity[4, "Inches"]], 
    Automatic}], ImageResolution -> 72]

enter image description here

It turns out that the important part is to not give ImageSize as an option to Export but as an option to Plot

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • I am confused here. I thought ImageSize was in printers points not pixels as you suggest. An Imagesize of 500 should come out at 6.94 inches as there are 72 points in an inch. I have a laptop and several docking stations so my screen resolution can vary. I am not clear how pixels which is about screen resolution comes into this. – Hugh Dec 04 '15 at 14:54
  • I don't really know, I just saw that when I set ImageSize to a particular value, then I looked at the exported file, it had that many pixels in its horizontal direction. – Jason B. Dec 04 '15 at 14:56
  • @Hugh - it seems that Export behaves differently depending on whether ImageSize is given directly as an option or as an option to Plot – Jason B. Dec 04 '15 at 15:05
  • A subtle point. I had not appreciated that there was a difference between usage of ImageSize in Plot and Export. Are the pixels screen pixels or some reference size? Would you get a poor picture because you are using a screen with few pixels? – Hugh Dec 04 '15 at 16:30
  • I'm not sure I follow you. Pixels should be the base unit from which an image's size is measured, literally how many little dots of color do you have. Then, how large the image appears depends on your screen resolution. Where am I wrong? – Jason B. Dec 04 '15 at 16:33
  • My screen has a resolution control. I can go from 800 by 600 to 1920 by 1080. Hence pixels don't have a physical size. Again in the documentation ImageResolution is given in printers points. I wonder if it is in pixels when used in Export. I feel the need to experiment. – Hugh Dec 04 '15 at 16:42
  • The more I think of this, I believe it makes sense. For Plot , which outputs to the screen, when I specify the size I can only mean how big is the image. But when I Export, I am making a file, and the only relevant size for a file is pixels, by which I mean how many bits of graphic data are written. Does that make sense? – Jason B. Dec 04 '15 at 19:33
  • Wait, so a .png file owns a parameter controlling its resolution? – xzczd Dec 05 '15 at 02:06
  • 1
    @xzczd, I think so. Take the files a500 and a72 and upload them to http://regex.info/exif.cgi – Jason B. Dec 05 '15 at 05:32
  • This is interesting. Seems to be an element that's not contained in Import[……, "Elements"]! – xzczd Dec 05 '15 at 12:32