3
$\begingroup$

I noticed that exporting a plot to PDF results in increased brightness relative to exporting the same plot to PNG.

Here are screenshots of a PDF and a PNG obtained from the same plot. As you can see, the picture above looks brighter than the one below.

enter image description here

enter image description here

Question. Is there a way to export to PDF while having the resulting picture look exactly as the corresponding PNG export?

I am using Mathematica 12.2.0 on a Mac (running macOS Monterey 12.5.1). To generate the plot and export the images I am using the code below.

SetDirectory[NotebookDirectory[]];

sphere[u_, v_] := {Cos[v] Cos[u], Cos[v] Sin[u], Sin[v]}

plot = ParametricPlot3D[
   Evaluate[sphere[u, v]], {u, -Pi, Pi}, {v, -Pi/2, Pi/2}, 
   PlotPoints -> 200, Mesh -> {30, 5}, MeshStyle -> Opacity[0.8], 
   PlotTheme -> {"Classic"}, Ticks -> None];

Export["sphere.pdf", plot];

Export["sphere.png", plot];

EDIT Aug 27, 2022. On my machine, exporting the PDF as suggested in Alexey Popkov's answer still results in a picture that looks brigther than the corresponding PNG (both with Preview and Adobe Acrobat Reader). If, on the other hand, I export the PDF by means of the Preview's export command, then the resulting PDF appears identical to the original PNG. Needless to say, I am puzzled.

EDIT Aug 29, 2022. The problem does not occur on the Windows computer in my office. So perhaps this is a macOS-related issue.

$\endgroup$
8
  • $\begingroup$ Please provide the code you are using to generate the plot. What version of Mathematica are you using? $\endgroup$ Aug 25, 2022 at 14:14
  • $\begingroup$ @AlexeyPopkov I have edited the question. $\endgroup$ Aug 25, 2022 at 17:00
  • 1
    $\begingroup$ Maybe I'm colorblind but the perceived difference in color might be due to the different thickness of the black lines due to different resolutions. Try: Export["sphere2.pdf", plot, ImageSize -> {360, 391}]; Export["sphere2.png", plot, ImageSize -> {360, 391}]; $\endgroup$ Aug 25, 2022 at 17:15
  • $\begingroup$ @RudyPotter On my screen sphere2.pdf still looks visibly brighter than sphere2.png. $\endgroup$ Aug 25, 2022 at 17:23
  • 2
    $\begingroup$ Perhaps a color space is being embedded into the PDF by default that is not being embedded into the PNG. $\endgroup$
    – Carl Lange
    Aug 25, 2022 at 17:31

1 Answer 1

6
$\begingroup$

Let us compare what is exported to PDF and PNG (I'm using Mathematica 12.3.1 on Windows 10 x64):

Export["sphere.pdf", plot];
Export["sphere.png", plot];
imgPDF = FirstCase[Import["sphere.pdf", "PageGraphics"], 
   r_Raster :> ImageReflect@Image[r], Automatic, -1];
imgPNG = Import["sphere.png"];
Information /@ {imgPDF, imgPNG}

out

We see that the main difference is in raster size of the images. Also, the image embedded in PDF has a transparency channel. Let us try to reproduce the image we obtained from PDF with PNG export:

Export["sphere.png", plot, RasterSize -> 1080, Background -> None];
imgPNG2 = Import["sphere.png"];

Information /@ {imgPDF, imgPNG2}

out

Excepting the resolution, other characteristics are identical. Let us check the ImageDifference:

ImageDifference[imgPDF, imgPNG2]

out

We see that the images are still different, probably due to different rendering methods used on the rasterization stage. Let us compare them more closely:

Image[ImageTake[#, {338, 443}, {228, 313}], 
   ImageSize -> 200] & /@ {imgPDF, imgPNG2}

out

Apparenlty, the renderings are quite different. I'm not sure which options allow to reproduce the image embedded in PDF. If you like it more, you could simply export it as PNG:

Export["sphere_from_PDF.png", imgPDF]

If you like the PNG variant more, you can use Rasterize with the corresponding settings to generate Image, and then export it as PDF:

Export["sphere_as_in_PNG.pdf", 
  img = Rasterize[plot, RasterSize -> 360, Background -> White]];

Now get the image back from PDF and compare with the original:

imgPDF2 = 
  FirstCase[Import["sphere_as_in_PNG.pdf", "PageGraphics"], 
   r_Raster :> Image[r], Automatic, -1];
ImageData[ImageDifference[img, imgPDF2]] // Abs // Max
0.

They are absolutely identical. Hence any visible differences are due to different rendering algorithms used by your image- and PDF-viewers. Here is a screenshot with the two images rendered on Windows 10 by Microsoft Photos and Adobe Acrobat Reader respectively:

screenshot

$\endgroup$
2
  • $\begingroup$ Thanks a lot for the detailed answer! On my screen the picture "sphere_as_in_PNG.pdf" is still visibly brighter than "sphere.png". Do the two pictures appear as identical on your machine? $\endgroup$ Aug 26, 2022 at 7:25
  • $\begingroup$ @MK7 I don't see any difference. I updated the answer with a screenshot showing both pictures. $\endgroup$ Aug 26, 2022 at 9:08

Your Answer

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

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