Graphics, Figures & TablesAvoid png resampling by pdfLaTeX

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Avoid png resampling by pdfLaTeX

Post by qubyte »

Hello all. This is my first post so I hope I'm not going to ask something stupid.

I have some code that is generating bitmaps for me (pseudocolor plots), and I'm using pgfplots to wrap the bitmap portion in nice axes etc. These bitmaps that I'm producing are saved as png files so that they don't suffer from compression. However, when pdflatex compiles a document with a png in it, it resamples the graphic. I'd like to be able to include the png file in such away that nothing is added or taken away. I'm a bit OCD about information being added or taken away from plots...

As a simple example:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{124_cropped}
\end{document}
The attached file is an example png. Compare the png with the generated pdf at the pixel level of the png.

Ultimately this is not a deal breaker for me, but it would be nice to see the plot I generated come out of the process without being mangled. If there is an easy way of avoiding this, perhaps by converting the png to a pdf in a lossless manner first, then I'd appreciate any advice.

Thanks!
Attachments
124_cropped.png
124_cropped.png (41.93 KiB) Viewed 4310 times
Last edited by qubyte on Thu Feb 10, 2011 7:55 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Re: Avoid png resampling by pdfLaTeX

Post by frabjous »

Does whatever program you're using to generate the PNG have the option to output to a vector format like PDF or EPS instead?
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Re: Avoid png resampling by pdfLaTeX

Post by qubyte »

I'm using Matlab for this, and it seems not without the same thing happening. Even if I could, I need to crop the output to get rid of whitespace and this is only practical with a bitmap as far as I know. I guess fiddling around with the bounding box of an eps is an option if I could get a nice plot in that format. I'll check this tomorrow at work.

That aside, I'd still like to know if there's a way of getting a png into a document without it being altered. :)
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Avoid png resampling by pdfLaTeX

Post by frabjous »

I've never noticed the problem you're discussing personally, so I don't know what's causing it, but I don't understand how the problem would also apply to eps or pdf figures, since the concept of "resampling" doesn't apply to vector graphics.

You should be able to crop pdf graphics using the pdfcrop perl script that came with your LaTeX distribution. (Personally I'd just convert eps to pdf for use with pdflatex using epstopdf from ghostscript, which you probably already have installed.) If you need a GUI, you could use BRISS.
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Avoid png resampling by pdfLaTeX

Post by qubyte »

Ah, you see, normally I'm a vector graphic purist. However, this is one of the few cases when a raster graphic is better. The bitmap contains exactly the right information, and a vector graphic version is only going to be bloated and scrolling the resultant pdf will be laggy. No, I want this to be raster graphic. PDF and EPS can serve as containers for those (of course) so this is the optimal solution.
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Re: Avoid png resampling by pdfLaTeX

Post by qubyte »

This is why I'm using pgfplots to contain the bitmap. Then I get all the vector graphic goodness from the box around the plot outwards. The best of both worlds! :)

I've attached an example pdf of what comes out of this arrangement. Using a vector version of the pseudo colour plot would have resulted in the pdf being several MB in size, and added nothing. Whilst this looks grainy, that's because I've had to calculate each pixel. A future version of this plot will be higher resolution most probably and even if it's not, this is how it is supposed to look.
Attachments
example.pdf
(67.44 KiB) Downloaded 215 times
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Avoid png resampling by pdfLaTeX

Post by localghost »

In case this represents a solution, please mark the topic accordingly as written in Section 3 of the Board Rules (to be read before posting).


Best regards and welcome to the board
Thorsten
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Re: Avoid png resampling by pdfLaTeX

Post by qubyte »

This is not a solution. The problem remains open; I've simply demonstrated what I'm doing and why it is important.

I read the board rules before posting. ;)

Cheers
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Re: Avoid png resampling by pdfLaTeX

Post by qubyte »

Ok, so I just took a look at this again, and it turns out there was a problem, just not where I was looking.

The problem is actually in Preview in OSX. It displays the png files properly, but with pdf files it interpolates for no reason. Opening this up in Acrobat Reader revealed that the pdf files that I've produced are in fact totally fine.

Macs... -_-
Post Reply