# MWE: Why are example-image-a.png and example-image-a.jpg of different size?

According to various tools the images example-image-a.png and example-image-a.jpg from the mwe package both have size 400 x 300 pixels. However, the document

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\noindent
\includegraphics{example-image-a.jpg}

\noindent
\includegraphics{example-image-a.png}
\end{document}


includes the pictures with different sizes. What determines the size at which the pictures are rendered, and why is it different in this case?

The JPEG file example-image-a.jpg does have resolution information:

Image file name = example-image-a.jpg

* File [example-image-a.jpg]
( FP-DIV ) ( FP-DIV )
Type: jpg
Pixel width: 400 px
Pixel height: 300 px
Density x: 90 dots per 72.27pt
Density y: 90 dots per 72.27pt
Width: 319.99911 bp
Height: 239.99905 bp


The density lines mean: 90 DPI

The PNG file lacks information about resolution:

Image file name = example-image-a.png

* File [example-image-a.png]
( FP-DIV ) ( FP-MUL ) ( FP-DIV ) ( FP-DIV )
Type: png
Pixel width: 400 px
Pixel height: 300 px
Ratio x: 90
Ratio y: 90
Width: 399.99916 bp
Height: 299.99965 bp


Therefore, a default resolution of 72 DPI is used.

The data are extracted with:

pdftex bmpsize-test


# Setting of the default resolution

In case of pdfTeX, the default resolution can be set via \pdfimageresolution. This resolution is used, if pdfTeX cannot find resolution data in the bitmap file. The graphics driver file pdftex.def supports the setting of this resolution by option resolution:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\noindent
\includegraphics{example-image-a.jpg}

\noindent
\includegraphics[resolution=90]{example-image-a.png}
\end{document}


# Extracting resolution data (pdfTeX)

The resolution data can also be extracted from example-image-a.jpg and used for example-image-a.png via the help of package bmpsize-base.

\documentclass{article}
\usepackage{graphicx}
\usepackage{bmpsize-base}

\makeatletter
% Macro \SetMacroToImageResolution{#1}{#2}
% #1: macro that gets the rounded DPI integer value as result
% #2: image file with extension
% Result: Macro #1 is defined with the
%   rounded image resolution in DPI as integer
%   or the resolution data are not complete.
\newcommand*{\SetMacroToImageResolutionX}[2]{%
\begingroup
\let\@ImageResolutionX\relax
% Try the supported image formats for pdfTeX in PDF mode
% until the correct image format is detected
\@tfor\x:={jpg}{png}\do{%
\expandafter\@TryGetImageResolutionX\expandafter{\x}{#2}%
\ifx\@ImageResolutionX\relax
\else
\@break@tfor
\fi
}%
\edef\x{\endgroup
\def\noexpand#1{\@ImageResolutionX}%
}\x
}
\newcommand*{\@TryGetImageResolutionX}[2]{%
\ifbmpsize@ok % Image could be read
\def\@ImageResolutionX{-1}%
% Test for complete resolution data
\ifx\bmpsize@unit\relax
\else
\ifx\bmpsize@pixelx\relax
\else
% Resolution data present
\edef\x{\strip@pt\dimexpr\bmpsize@unit}%
\FPset{\x}{\x}%
\FPmul{\x}{\x}{\bmpsize@pixelx}%
\FPdiv{\x}{\x}{72.27}%
\FPround{\x}{\x}{0}%
\let\@ImageResolutionX\x
\fi
\fi
\fi
}
\makeatother

\begin{document}

\noindent
\includegraphics{example-image-a.jpg}

\SetMacroToImageResolutionX\ImageResolution{example-image-a.jpg}

\noindent
\includegraphics[resolution=\ImageResolution]{example-image-a.png}
\end{document}

• Good to know that the graphics driver respects DPI metadata. I've actually encountered situations with files saved from the internet where the DPI ratio was corrupt or not present, causing the image to appear as a vertical or horizontal line in some viewers, so that's useful. (I ended up writing a small binary-editor script to fix that for JPEGs, but I seem to have lost it, oh well.) – JAB Oct 11 '16 at 20:04
• @heiko-oberdiek is there a way to make it have the resolution of another graphic? – Mystic Odin Oct 12 '16 at 10:37
• @MysticOdin See updated answer. – Heiko Oberdiek Oct 12 '16 at 17:49