# preserving pixel resolution when converting to PDF

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.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[x=0.015625in, y=0.015625in]
\node (plot) at (128.0,128.0)
{\includegraphics{plot_0004.png}};

\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 ...

\end{tikzpicture}
\end{document}

Here is a zoomed in view of the original PNG figure :

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.

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?

• Have a look at tex.stackexchange.com/questions/1162/…, maybe it helps you. – Uwe Ziegenhagen May 13 '17 at 19:19
• 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
• 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