【问题标题】:How to change the print size of an image in mm on command line?如何在命令行上以 mm 为单位更改图像的打印尺寸?
【发布时间】:2012-08-03 19:31:47
【问题描述】:

ImageMagick 似乎最好在命令行上转换图像文件。但是,它只支持更改像素大小和每像素英寸分辨率,但不支持打印大小(如命令identify -verbose 所示)。我愿意:

  • 快速获取图片打印尺寸mm
  • 更改图像打印尺寸(通过将高度或宽度或两者都设置为以毫米为单位的新值)

这应该可以通过简单的 shell 脚本来实现,不是吗?

【问题讨论】:

    标签: shell imagemagick image-manipulation


    【解决方案1】:

    图像的唯一绝对维度是像素

    Resolutionmmdensityresolution 只有在您渲染 特定表面上的图像(屏幕显示、纸张打印输出)。

    这些都有自己的内置的、硬件相关的分辨率。如果您知道,您可以计算图像尺寸的 mm 值,前提是您希望以“自然尺寸”呈现它。

    您通常不想要“自然尺寸”——有时您可能想要:“用图像填充 Letter 大小的纸张”(缩放以适合)。如果发生这种情况,则必须放大或缩小相同的图像 - 但屏幕或打印机分辨率不会改变,只是插值算法将开始添加像素以填充间隙(放大)或删除像素以使图片看起来更小(按比例缩小)。

    因此,在有人给出有关如何计算“以 mm 为单位的图像尺寸”(以图像的自然尺寸)的算法之前,您需要知道目标设备(屏幕或打印机)的分辨率。


    编辑:

    如果您将给定的图像(其大小以像素为单位)嵌入 PDF(源文档来自 LaTeX),您仍然需要指定...

    • ...您希望在页面上呈现图像的分辨率
    • ...或者您希望图像呈现的 尺寸(以毫米或页面尺寸的百分比为单位)。

    如果不对图像重新采样,您无法同时确定两个这两个参数。选择一个,另一个由你的选择隐含地决定。

    举个例子。

    假设您的原始图像为 2160x1440 像素。

    您的 LaTeX -> PDF 转换由 Ghostscript 完成。 Ghostscript 在内部对所有光栅对象使用 720 dpi 的默认分辨率。因此,除非您为 PDF 转换明确设置“分辨率”为不同的值,否则 PDF 或打印页面上的图像大小将为 3x2 英寸(76.2 x 50.8 毫米)。

    如果您将分辨率设置为 90 dpi,则页面上的图像尺寸将为 24x16 英寸(609.6 x 406.4 毫米)。

    如果您将分辨率设置为 270 dpi(接近常用的 300 dpi),则图像尺寸将转换为 8x5.333 英寸(203.2 x 135.5 毫米)。

    所以shell脚本的公式是:

     # 25.4 mm == 1 inch
    
      image_width_px=W                      # in pixels (integer)
     image_height_px=H                      # in pixels (integer)
     resolution=R                           # in pixels/inch
    
      image_width_in_inch=$((W / R))        #   Shell arithmetics: does only handle
     image_height_in_inch=$((H / R))        #+  and return integers!
    
      image_width_in_mm=$(( $((W / R)) * 254/10 ))
     image_height_in_mm=$(( $((H / R)) * 254/10 ))
    
     # use 'bc' to achieve higher precision arithmetics:
      precise_image_width_in_mm=$( echo \
                                   "$image_width_px  / $resolution * 25.4" \
                                   | bc -l )
     precise_image_height_in_mm=$( echo \
                                   "$image_height_px / $resolution * 25.4" \
                                   | bc -l )
    

    【讨论】:

    • 好吧,如果我通过 LaTeX 在 PDF 中包含图像,则存在纸张大小。也可以在 Gimp 中修改这个“图像大小””如何不用 image-magick?
    • 是的,有纸张尺寸。但是你至少告诉你的转换过程这两个中的一个:(1)它应该为图像使用哪种分辨率?这隐含地设置了页面上的图像大小。 (2) 图像应该覆盖哪个区域?这隐含地设置了图像分辨率。 -- 如果你想设置both(除非它们完全匹配)你会得到一个不同的图像,与原始图像相比具有不同的像素数....
    • 谢谢!不仅如此,我只需要找出 hob 即可通过相应的命令行参数获取和设置值。我希望有人已经这样做了。
    • 如果您只是问一个关于您想让 ImageMagick 在您的图片上执行的任务以及您想要“以毫米为单位提供尺寸”的统计信息,这不是更好吗?
    • 对不起,虽然我已经问得很清楚了。随意编辑我的问题!
    【解决方案2】:

    我尝试用my own script in Perl 解决它。正如 Kurt Pfeilfe 在他的回答中所解释的那样,必须根据像素大小和请求的打印大小来计算每英寸的点数。

    sub getsize {
      my $file = shift;
      my $info = do { # avoid the shell, no escaping needed
        my @args = ('-format','%G %x%y','-units','PixelsPerInch',$file);
        open my $fh, "-|", "identify", @args or die "$!\n";
        <$fh>;
      };
      if ($info =~ /^(\d+)x(\d+) (\d+) PixelsPerInch(\d+) PixelsPerInch/) {
        my ($px,$py,$dx,$dy) = ($1,$2,$3,$4);
        my ($sx,$sy) = map { $_ * 25.4 } ($px/$dx, $py/$dy);
        return ($px,$py,$dx,$dy,$sx,$sy);
      } else {
        die $info;
      }
    }
    
    foreach my $file (@ARGV) {
      if ($file =~ /^(\d*)(x(\d+))?mm$/) {
        ($mx,$my) = ($1,$3);
      } elsif( -e $file ) {
        my ($w,$h);
        if ($mx || $my) {
          my ($px,$py,$dx,$dy,$sx,$sy) = getsize($file);
          my $rx = 25.4 * ( $mx ? ($px/$mx) : ($py/$my) );
          my $ry = 25.4 * ( $my ? ($py/$my) : ($px/$mx) );
          system qw(convert -units PixelsPerInch -density),
              sprintf("%.0fx%.0f",$rx,$ry), $file, $file;
        }
        printf "$file: %dx%d at %dx%ddpi = %dx%dmm", getsize($file);
      } else {
        die "file not found: $file\n";
      }
    }
    

    脚本不支持毫米的小数部分,请随意修改the source

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2014-10-31
      • 2017-02-19
      • 2013-11-17
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多