When I include bitmap images in my PDF files (created with pdflatex
and the graphicx
package), I always check manually that the resolution will be suitable (I shoot for something between 200 and 300 dpi). I just realized it’s a pity to do it by hand, while LaTeX actually knows both the pixel size of the image and its final size on paper.
Is it possible to have LaTeX issue a warning when the resolution of an included bitmap image will be outside certain bounds?
Best Answer
LaTeX neither knows the resolution nor the final size on the paper:
\includegraphics
prints the requested sizes in the.log
file with driverpdftex.def
:But
\includegraphics
can be put inside\scalebox
or\resizebox
, for example. Thus the final size on the paper is not known, when\includegraphics
is executed.The terminal output and the
.log
file also contain something like:That is printed by the driver part of pdfTeX. It is the natural size of the image that would be the size of the image, if the image is included by pdfTeX without further scaling.
\pdfimageresolution
is only considered, if the image does not contain resolution data.What can be done:
Reading the
.log
file by a script that takes the natural size, the requested size, the image file name. Then it can calculate the scale factor and can apply it to the resolution. The script gets the resolution by running an external program (e.g.file
in Linux) or by analyzing the image. But this method will not detect later scalings.Package
bmpsize
analyzes bitmap images. The method there can be used to get the resolution at TeX macro level. The package itself terminates for pdfTeX in PDF mode, originally because it is not needed to calculate the size of the image, that pdfTeX in PDF mode already is able to do itself. Again, later scalings are difficult to track.Parsing the PDF file directly. Then the settings of the current transfer matrix can be tracked and applied to the resolution of the bitmap image data. Also this method can find images inside PDF Form XObjects, that are missed by the previous methods (pdfTeX can include PDF files that contain bitmap images).