I have a latex file that overlays tikz output onto a PNG file. The PNG file is a 512x512 pixel image which perfectly represents raster data (via a colormap) from an external data set. The tikz file draws rectangles around groups of pixels in the PNG file. These boxes represent leaves in a quadtree data structure.

Using matplotlib, I can produce the PNG file with exactly the correct size (e.g. 4in by 4in), and DPI which exactly presents the raster data (128 DPI). The tikz drawing is then scaled to perfectly overlay this image.

The problem is, though, when I use pdflatex on the latex document which overlays the tikz, the resulting PDF file blurs the pixel boundaries. I am using the standalone package, and the final image is exactly 4in x 4in (nearly), so there shouldn't be any scaling to fit some fixed page width.

Here is my tikz file (shortened for clarity ..). In the tikz image, 1 unit = 1/64in, so that the final image is 256/64 = 4in.


\begin{tikzpicture}[x=0.015625in, y=0.015625in]
    \node (plot) at (128.0,128.0)

    \draw [ultra thin] (0,0) rectangle (256,256);  % boundary box

    % Patch number 0
    \draw [ultra thin] (0,0) rectangle (16,16);

    % Patch number 1
    \draw [ultra thin] (16,0) rectangle (32,16);

    % ..... more rectangles ...


Here is a zoomed in view of the original PNG figure : enter image description here

and here is a zoom of the resulting PDF file with the tikz overlay. Each tikz rectangle in this zoom represents an 8x8 grid of pixels in the PNG file. enter image description here

Is there any way to preserve the original resolution in the PNG file? I realize I can bump up the resolution of the PNG file (make it 1024x1024, for example) which reduces the blurring, but this seems unnecessary, since the 512x512 exactly represents my data.

I am viewing both the PNG and the PDF in Preview.app. The images above are screen shots taken from the Preview viewer. So it seems that Preview is able to view the PNG in its native resolution (possibly by converting it to an internal PDF representation?). Why can't Latex to do the same thing?

| improve this question | | | | |
  • 1
    Have a look at tex.stackexchange.com/questions/1162/…, maybe it helps you. – Uwe Ziegenhagen May 13 '17 at 19:19
  • 1
    LaTeX and pdfLaTeX are not able to change the image data. Maybe, the PDF viewer has activated some anti-alias settings. – Heiko Oberdiek May 13 '17 at 20:17
  • @UweZiegenhagene : Thanks for linking that question. There were some helpful tips, but nothing I could make work. I don't need to adjust the resolution - I want the pixels to remain sharp. – Donna May 13 '17 at 20:36
  • @HeikoOberdiek : Hm.. but it doesn't do the anti-aliasing when I view the PNG file using the same viewer. – Donna May 13 '17 at 20:38
  • 1
    Could you try with a PDF reader that lets you explicitly disable any smoothing? This does look similar to tex.stackexchange.com/questions/10975/… – Jake May 13 '17 at 20:54

It seems that the problem was in fact with the viewer. I just downloaded Adobe Reader (for OSX), and loaded the same image, and all of the pixel detail shows up clearly. In fact, the Adobe viewer has many more options for adjusting the smoothing than Preview (no surprise!).

Here is the same image in Adobe (with a custom DPI setting to 128) : This image was also grabbed as a PNG screenshot (same as those above).

enter image description here

I wasn't convinced that the blurriness had much to do with the viewer, but now I am converted!

| improve this answer | | | | |
  • 1
    Indeed. Different PDF viewers have VERY different properties. There are tradeoffs between speed of rendition and technical accuracy. On the other hand, Adobe Reader will complain and fail to display erroneous files that some other viewers will display without complaint (they ignore the error). – user103221 May 14 '17 at 1:42
  • @RobtA I was disappointed to see that Adobe wouldn't open my PNG file, whereas Preview had no trouble. I'd be curious to know what Preview does with the PNG. Does it convert it to PDF? If so, why doesn't it then apply smoothing? – Donna May 14 '17 at 8:23
  • 1
    I don't know about Preview. But I know that Adobe Reader "by intent" fails to open files that have certain technical issues, even though the issues are very minor, and the understandable PDF can be interpreted. This is a way to test that a PDF file is in-spec (as far as Adobe is concerned) without using a non-free professional tool. Might be nice if Adobe Reader had an "ignore issues" option, but AFAIK it doesn't. Perhaps a tool such as pdftk could repair your PDF. – user103221 May 14 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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