1

I was sent a png image by an author (Janusz Bie\'n) and converted to jpg (pngtopnm Rubricella.png | pnmtojpeg >Rubricella.jpg) to reduce file size (jpg lossiness not an issue here). The resulting images are exactly the same size in pixels (width=2568 height=4221). I discovered to my surprise that \includegraphics clips the original png and new jpg differently. Example:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\leftline{png: \includegraphics[width=2.2in,trim=15 15 15 15,clip]
  {Rubricella.png}}
\bigskip
\leftline{jpg: \includegraphics[width=2.2in,trim=15 15 15 15,clip]
  {Rubricella.jpg}}
\end{document}

I'll attach a screenshot of the result. One can see that the original png is clipped tightly to the page image (as intended), while the jpg shows black borders, i.e., not nearly as much got clipped with the jpg. I'm not sure if any clipping happened at all.

If I run pngtopnm Rubricella.png | pnmtopng >x.png and then include x.png, the clipping is likewise minimal/nonexistent. So the culprit isn't the conversion to jpg, but that something is different about the original image.

Since all image pixel sizes are the same ... what else can affect the clipping? Math overflow? I didn't see any messages in the log or on the terminal.

The obvious difference is that the original image file isze is over 20mb; the converted images, whether png or jpg, are closer to 2mb. Why the file size should matter when the pixel dimensions are the same, or why the clipping works with the larger image and not the smaller, is mysterious to me. I've tried the same sort of tests with other random images, and never seen a difference.

The original and converted images are over the limit here, so I put them at https://tug.org/~karl/Rubricella.png and https://tug.org/~karl/Rubricella.jpg.

This is with pdflatex. Maybe it is something in pdftex's reading of this particular png?

clipping comparison images

4
  • Are you using ImageMagick or GraphicsMagick for the conversion? If so, see what happens when you add -strip to the command line. This may or may not be useful, but it is easy to do. Rationale: An image may contain special format instructions in its metadata, which -strip removes.
    – rallg
    Oct 29, 2023 at 17:24
  • 1
    well the png has a dpi setting (300x300) and the jpg not, which means a dpi of 72 or something like that is assumed. If you insert the graphics in their original size you will see that they have quite different size (Rubricella.jpg, id=11, 2577.63pt x 4236.82875pt versus Rubricella.png, id=1, 618.6312pt x 1016.8389pt), and so naturally the trim affects them differently too. If you set \pdfimageresolution 300 the images have the same size. Oct 29, 2023 at 19:03
  • Pursuant to the above comment by @UlrikeFischer, you can add the image resolution using *magick.
    – rallg
    Oct 29, 2023 at 20:00
  • Thank you both. Ulrike, your answer seems complete (I would never have guessed that internal image metadata would affect latex-level manipulation), but I see no "green checkmark" to accept it ...
    – Karl Berry
    Oct 30, 2023 at 20:06

1 Answer 1

3

Pixels have no size, you need to take the resolution into account. The png has a internal dpi setting of 300x300. The jpg doesn't have one, which means that with pdftex a dpi of 72 is assumed (see e.g. the pdftex manual).

So if you insert the graphics in their original size you will see that they have quite different sizes: The jpg has a width around 35.6in = 2568 dots / 72 dpi. (Rubricella.jpg, id=11, 2577.63pt x 4236.82875pt) and the png a width of around 8.5in (Rubricella.png, id=1, 618.6312pt x 1016.8389pt), and naturally the trim affects them then differently too.

If you set \pdfimageresolution=300 or if you add a resolution of 300dpi to the jpg the images will have the same size.

You must log in to answer this question.

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