如何以编程方式获取图像的纵横比

如何以编程方式获取图像的纵横比

如何获取图像的纵横比并将其存储在 pgf 宏中以供进一步计算?例如:

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\includegraphics{image.jpg}

\begin{tikzpicture}
\pgfmatsetmacro{\aspectratio}{???} % This variable should contain the aspect ratio of image.jpg
\end{tikzpicture}
\end{document}

答案1

这是一个\aspectratio宏,它可以打印纵横比或将其存储在作为可选参数给出的宏中。 * 形式将执行全局分配,以防您需要它。 宏(在示例中为\test)可用于\pgfmathsetmacro或其他接受浮点数的环境。

\documentclass{article}
\usepackage{xparse} % also loads expl3
\usepackage{graphicx}

\ExplSyntaxOn
\NewDocumentCommand{\aspectratio}{smo}
 {% #2 is the image file
  \hbox_set:Nn \l_tmpa_box {\includegraphics{#2}}
  \IfNoValueTF{#3}
   {
    \__student_aspectratio:nn { \box_wd:N \l_tmpa_box } { \box_ht:N \l_tmpa_box }
   }
   {
    \IfBooleanTF{#1}{ \tl_gset:Nx } { \tl_set:Nx } #3
     {
      \__student_aspectratio:nn { \box_wd:N \l_tmpa_box } { \box_ht:N \l_tmpa_box }
     }
   }
 }

\cs_new:Nn \__student_aspectratio:nn
 {
  \fp_eval:n {round( #1 / #2 , 5)}
 }
\ExplSyntaxOff

\begin{document}

\aspectratio{example-image}

\aspectratio{example-image-1x1}

\begingroup
\aspectratio*{example-image-9x16}[\test]
\endgroup

\texttt{\meaning\test}

\end{document}

在此处输入图片描述

答案2

pdftex可以使用寄存器\pdfximagebbox来检索边界框的坐标。这些是

在此处输入图片描述

图像的宽度和高度显然由以下公式给出

width  = [upper-right x] - [lower-left x]
height = [upper-right y] - [lower-left y]

并且纵横比只是width/height。使用xfp包来评估浮点表达式,以下文档打印1.333...在图像下方(其纵横比为 4:3,因此是正确的)。

\documentclass{article}
\usepackage{graphicx,xfp}
\begin{document}

\includegraphics{example-image-a}

\fpeval{
  (\pdfximagebbox\pdflastximage 3 - \pdfximagebbox\pdflastximage 1)
  /
  (\pdfximagebbox\pdflastximage 4 - \pdfximagebbox\pdflastximage 2)
}

\end{document}

答案3

\newcount这是一个使用寄存器存储长度的技巧。

\documentclass{article}
\usepackage{graphicx}
\usepackage{fp}
\newcount\boxheight
\newcount\boxwidth
\newcommand\testaspect[1]{%
  \setbox0=\hbox{#1}%
  \boxheight=\ht0\relax%
  \boxwidth=\wd0\relax%
  \FPdiv\theaspect{\the\boxheight}{\the\boxwidth}%
  \copy0%
}
\begin{document}
\testaspect{\includegraphics{example-image}}

The aspect ratio is \theaspect.

\testaspect{A}

The aspect ratio is \theaspect.

\testaspect{\tiny A}

The aspect ratio is \theaspect.
\end{document}

在此处输入图片描述

答案4

\SetToRatiohttps://tex.stackexchange.com/a/383700/4686

\documentclass{article}

\usepackage{graphicx}
%\usepackage{tikz}

\newsavebox\mybox

% https://tex.stackexchange.com/a/383700/4686
\makeatletter
\newcommand\SetToRatio[3]{% sets #1 to be the ratio #2/#3, where #2 and #3 
    % are lengths (registers or expressions).
    % The ratio #2/#3 should evaluate to less than 16384 in absolute value to 
    % avoid arithmetic overflow. It will be computed as fixed point
    % number with about 4 or 5 digits after decimal mark.
    \edef #1%
        {\strip@pt\dimexpr
         \numexpr\dimexpr#2\relax*65536/\dimexpr#3\relax\relax sp\relax}%
}
\makeatother
\begin{document}

\sbox{\mybox}{\includegraphics[draft]{example-image.pdf}}    
% you may prefix this with \global if executed within scope limiting location
\SetToRatio\aspectratio{\dimexpr\ht\mybox+\dp\mybox\relax}{\wd\mybox}

% debugging
% \show\aspectratio

% now use it !
%\begin{tikzpicture}
%\pgfmatsetmacro{\aspectratio}{???} % This variable should contain the aspect ratio of image.jpg
%\end{tikzpicture}
\end{document}

相关内容