如何使用 pnmscale 缩放图像的较长边并保持比例?

How to use pnmscale to scale the longer side of an image and still keep the ratio?

我正在编写一个 GNU Makefile 来对图像进行一些处理。 一项任务是使用 pnmscale 命令通过 SIZE 参数缩放图像(.ppm 格式)。图像应按长边缩放而不丢失比例,并应保存在 .scaled 下。

我已经阅读了 pnmscale 的手册页,但似乎找不到正确的选项。

我试过以下方法:

pnmscale -pixels 100 example.ppm > example.scaled

example.ppm 的大小为 200 x 100 pixels 且我 运行 大小为 100 pixels 的 pnmscale 命令时,example.scaled 的大小应为 100 x 50 pixels。使用我的解决方案,图像变得非常小。

manpage of pnmscale 所述,选项 pixels

specifies a maximum total number of output pixels. pnmscale scales the image down to that number of pixels. If the input image is already no more than that many pixels, pnmscale just copies it as output; pnmscale does not scale up with -pixels.

换句话说,通过指定 -pixels 100,您实际上是将图像缩小到最大 100 像素。您想要实现的是将输入图像缩小到 100 x 50 pixels = 5000 pixels.

的大小

再次查看 manpage of pnmscale 得到以下结果:

pnmscale [{-xsize=cols | -width=cols | -xscale=factor}] [{-ysize=rows | -height=rows | -yscale=factor}] [pnmfile]

[...]

If you specify one dimension as a pixel size and don't specify the other dimension, pnmscale scales the unspecified dimension to preserve the aspect ratio.

在你的情况下,使用

pnmscale -xsize 100 example.ppm > example.scaled

应该将您的输入图像缩小到 100 像素的宽度。