17

I have a number of images which I want to include in my document with \includegraphics (or other similar thing).

The catch is that I want to explicitly override the DPI of particular images on a case-by-case basis.

That is, I want to do something like this:

\newcommand{\smalldpi}{100}
\newcommand{\largedpi}{600}

\includegraphics{foo.png}% default DPI seems to be fine for this one
\includeapicture[dpi=\smalldpi]{bar.png}
\includeapicture[dpi=\largedpi]{baz.png}

This documentation doesn't seem to mention DPI at all.

How can I have an image's physical size be determined from its pixel dimensions and a given DPI?

14
  • 1
    you can use \pdfimageresolution primitive in pdftex, but why not just use scale=6 or whatever scale factor you want? Commented Jul 9, 2017 at 20:50
  • 1
    the original image data is (usually) just copied to the file and if (for interlacing or gamma correction etc) it is unpacked and repacked it is done losslessly to put exactly that bitmap data in the pdf stream, any rescaling is just done by wrapping that in a pdf coordinate scale so any resampling os fone by the pdf renderer not by tex (@berkus) Commented Jul 9, 2017 at 21:43
  • 1
    it's defined but the system is just going to take the ratio of the dpi and apply a scale factor of that amount, if you want the final image to be 2in wide it seems simplest to use [width=2in] than try to specify a nominal dpi that differs from the image specified dpi (if it has one) or pdftex's normal default dpi of 72 by a scale factor that results in the final image being 2in wide. Commented Jul 9, 2017 at 21:47
  • 3
    @DavidCarlisle you say " if you want the final image to be 2in wide it seems simplest to use..." but that's the thing: I don't know I want it to be 2in wide, I know I want it to be pixelwidth/dpi inches wide. I don't want to calculate it by hand because the image files themselves are not yet determined -- but their desired DPI is determined.
    – spraff
    Commented Jul 9, 2017 at 22:32
  • 1
    historically tex couldn't access that information but in pdftex, with png, you can use \pdfimageresolution=100\includegraphics{file.png} to do what I think you want to do it is just a very rare thing to want to do (I can't recall anyone asking for a more latex-like interface to that in all the 35 or so years the graphics package has been available:-) Commented Jul 9, 2017 at 22:46

1 Answer 1

5

Solution using pdfTeX primitive and a macro:

In the preamble:

\newcommand{\includegraphicsdpi}[3]{
    \pdfimageresolution=#1  % Change the dpi of images
    \includegraphics[#2]{#3}
    \pdfimageresolution=72  % Change it back to the default
}

In the document:

\includegraphicsdpi{300}{}{figures/rpi/app_main}

Old solution: I used to use \includegraphics[resolution=300]{bar.png} to do exactly this, using Texlive 2016. Maybe that works for you. I recently updated Texlive, and it stopped working. Getting Package keyval Error: resolution undefined. Separate question about that, here.

3
  • Not that my macro does not work if you include the same image twice. If you do that, the resolution of the first invocation will be used. Don't know why. GLHF. Commented Sep 22, 2017 at 12:45
  • 1
    Instead of changing it back, simply wrap the first two macros in a { } group. Commented Dec 21, 2017 at 13:32
  • Is this answer still valid? Calling pdfimageresolution doesn't seem to change anything :(
    – GURKE
    Commented Jan 24, 2023 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .