4

Note: This is not a duplicate of How to save a figure produced by tikz save/export as JPG/PNG file. This question deals with Ghostscript and quality concerns. The other question does not deal with Ghostscript, so none of the discussion at the other question is about quality considerations related to Ghostscript. Further, in the comments section, there is a good comment which should be posted as an answer.


Here is my code:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[xmin=0, xmax=4, xlabel=x, ylabel=y]
    \addplot[domain=0:4, samples=100, blue, thick] {cos(deg(x))};
\end{axis}

\end{tikzpicture}
\end{document}

When I compile this with pdflatex foo.tex I get a good quality PDF. Here is a screenshot of the PDF:

enter image description here

Now I try to convert the PDF into PNG. Here is my first attempt with -sDEVICE=png16m:

gs -sDEVICE=png16m -sBATCH -sNOPAUSE -r300 -sOutputFile=foo1.png foo.pdf

The output is of pretty disappointing quality. The curve appears to be pixelated. The slopes seem like staircase. It looks like this:

enter image description here

Here is my second attempt with -sDEVICE=pngalpha:

gs -sDEVICE=pngalpha -sBATCH -sNOPAUSE -r300 -sOutputFile=foo2.png foo.pdf

The output is much better but the background is transparent:

enter image description here

My questions:

  1. Why does the png16m device produce so much poorer quality output than pngalpha?
  2. How do you produce good quality images from pgfplots using Ghostscript?
Lone Learner
  • 3,226
  • 24
  • 44
  • May be try the solution of this post : https://tex.stackexchange.com/questions/91472/how-to-save-a-figure-produced-by-tikz-save-export-as-jpg-png-file – Maxouille Sep 03 '18 at 12:53
  • 4
    PNG being a purely bitmapped format as opposed to PDF, visual quality of the PNG output depends on the resolution and the scaling/zooming level at which it is viewed on screen or on paper. Using gs you can play with the resolution set via the -r ... option. – AlexG Sep 03 '18 at 12:53
  • Try gs -sDEVICE=png16m -sBATCH -sNOPAUSE -r300 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=foo1.png foo.pdf – David Purton Sep 03 '18 at 13:13
  • @AlexG I have used the exact same -r setting for both the gs commands I have mentioned but I get very different outputs. Why is that? Why is it that choosing png16m as the device leads to inferior results than choosing pngalpha? – Lone Learner Sep 03 '18 at 14:59
  • @DavidPurton Your command improves the output significantly but the output is still slightly inferior to the one produced by -sDEVICE=pngalpha. Can you elaborate in an answer why -dTextAlphaBits=4 -dGraphicsAlphaBits=4 improves the output? – Lone Learner Sep 03 '18 at 15:01
  • 1
    @LoneLearner The resolution is the same in both cases, judged from the stepping of the curve at high zoom. The visual quality improvement of -sDEVICE=pngalpha is accomplished through antialiasing, that is, setting a blended colour in the step corners. – AlexG Sep 03 '18 at 15:08
  • @AlexG Do you know why png16m does not perform antialiasing? I was expecting both png16m and pngalpha to perform similar antialiasing. The only difference I was expecting between them is the presence or lack of alpha channel. But it appears that png16m and pngalpha work very differently. – Lone Learner Sep 03 '18 at 15:51
  • 2
    First of all you have asked two unrelated questions. Now you have changed the title of the question as well. Now the title and question 1 are unrelated to LaTeX and question two is answered in the "duplicate question". – Stefan Pinnow Sep 06 '18 at 11:05
  • If you think that the quality of the PNGs you get using Togh's answer there you will get images of higher quality if you increase the value of the -density option to your needs. – Stefan Pinnow Sep 06 '18 at 11:07
  • @StefanPinnow I have edited question 2 too as well now to be consistent with the title and question 1. There were already 89 questions tagged ghostscript when I posted this, so I thought questions about related software and tools such as ghostscript for the purpose of conversion of LaTeX output were on topic here. If that's not the case, is it possible to move the question to an appropriate Stack Exchange site? – Lone Learner Sep 06 '18 at 11:18
  • Ok, now that the question is consistent and not answered by the given question in your questions note I reopened your question. I don't want to decide (alone) if this question is off-topic or not. So for the moment I would say it is perfectly fine here ;) – Stefan Pinnow Sep 06 '18 at 11:41

1 Answers1

5

The apparent quality improvement with -sDEVICE=pngalpha as compared to -sDEVICE=pn16m is achieved by application of anti-aliasing.

According to https://ghostscript.readthedocs.io/en/latest/Devices.html#png-file-format , with -sDEVICE=pngalpha antialiasing is enabled by default:

The png16malpha and pngalpha devices are 32-bit RGBA color with transparency indicating pixel coverage. The background is transparent unless it has been explicitly filled. PDF 1.4 transparent files do not give a transparent background with this device. The devices differ, in that the pngalpha device enables Text and graphics anti-aliasing by default. We now recommend that people use the png16malpha device in preference, and achieve any required antialiasing via the DownScaleFactor parameter, as this gives better results in many cases.

For the png16m output device, antialiasing must be enabled explicitly for text and graphics. Add options -dTextAlphaBits=4 -dGraphicsAlphaBits=4:

-dTextAlphaBits=n
-dGraphicsAlphaBits=n

These options control the use of subsample antialiasing. Their use is highly recommended for producing high quality rasterizations of the input files. The size of the subsampling box n should be 4 for optimum output, but smaller values can be used for faster rendering. Antialiasing is enabled separately for text and graphics content.

AlexG
  • 54,894