默认分辨率的设置

默认分辨率的设置

根据各种工具,图像example-image-a.png和包example-image-a.jpg中的图像mwe尺寸均为 400 x 300 像素。然而,文档

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

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

包括不同大小的图片。什么决定了图片渲染的大小,为什么在这种情况下会有所不同?

在此处输入图片描述

答案1

JPEG 文件example-image-a.jpg确实有分辨率信息:

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

这些density线条的意思是:90 DPI

PNG 文件缺少有关分辨率的信息:

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

因此,默认决议72 DPI用来。

数据提取如下:

pdftex bmpsize-test

默认分辨率的设置

对于 pdfTeX,默认分辨率可以通过 来设置\pdfimageresolution。如果 pdfTeX 在位图文件中找不到分辨率数据,则使用此分辨率。graphics驱动程序文件pdftex.def支持通过选项 来设置此分辨率resolution

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

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

结果

提取分辨率数据(pdfTeX)

还可以通过包的帮助从中提取example-image-a.jpg并使用分辨率数据。example-image-a.pngbmpsize-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 0 if the image is not found
%   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]{%
  \csname bmpsize@read@#1\endcsname{#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}

相关内容