【问题标题】:Getting the height and width of an image获取图像的高度和宽度
【发布时间】:2013-09-15 15:22:04
【问题描述】:

要将图像放置在带有颜色图的 PDF 中,请使用图像的高度和宽度。是否有可能在后记本身中获得这些值?那么当图片替换为不同尺寸的图片时,您不必更改任何内容。

【问题讨论】:

标签: postscript


【解决方案1】:

使用乔治的假设,即您正在尝试提取 jpg 文件的尺寸,我在链接到 this page with example code (C) 的此站点上找到了 this answer

这导致了这个小sn-p。请注意,如果 jpg 文件包含缩略图,这将失败(它将返回缩略图的尺寸而不是完整图像)。

%!

% (filename.jpg)  jpgdims  width height true
%                          false
/jpgdims {
    (r) file dup   % f f 
    200 string     % f f buf 
    readstring pop % f buf 

    <FFC0> search {  % f post match pre 
        pop pop           % f post
        exch closefile    % post
        3 4 getinterval   % post(3,4)
        {} forall         % post_3 post_4 post_5 post_6
        exch 256 mul add  % post_3 post_4 post_5*256+post_6
        3 1 roll          % post_5*256+post_6 post_3 post_4
        exch 256 mul add  % post_5*256+post_6 post_3*256+post_4
        true              % width height true
    }{  
        closefile
        false
    } ifelse

} def 

(ray-0.jpg) jpgdims

附加参考: wikipedia's JFIF page.

使用另一种脚本语言来生成 postscript 代码可能更简单,您可以在这种语言中对 ImageMagick 的 identify 程序进行外壳处理,并解析其文本输出。

$ identify ray-0.jpg
ray-0.jpg JPEG 320x200 320x200+0+0 8-bit PseudoClass 256c 411B 0.000u 0:00.000

【讨论】:

  • 我会试试你的代码。但是使用另一种脚本语言的想法也是一个不错的想法。目前我已经使用 Bash 进行了一些预处理,所以我也可以在其中使用它。
【解决方案2】:

不确定您在这里问的是什么,图像的尺寸是行数和列数,而不是图像的“大小”。 Matrix 确定了从用户空间到设备空间的映射,从而确定了打印区域。

您还同时询问 PostScript 和 PDF,您的意思是什么? PostScript 中的图像操作数与 PDF 中的图像操作数大体相同。

【讨论】: