命令行基础知识:使用ImageMagick调整图像大小

If you’ve ever done programmatic image manipulation (especially in PHP) you have probably encountered the ImageMagick library or it’s major fork, GraphicsMagick. In addition to being able to leverage it’s power from many popular programming languages, you can also use it directly from the command-line thanks to the library’s supplied executables like mogrify and convert.

如果您曾经做过程序化的图像操作(尤其是在PHP中),则可能会遇到ImageMagick库或它的主要分支GraphicsMagick 。 除了可以利用许多流行的编程语言提供的功能外,由于库提供的可执行文件(例如mogrifyconvert ,您还可以直接从命令行使用它。

入门 (Getting started)

The ImageMagick library is quite popular, but doesn’t usually come installed by default. Thanks to it’s popularity though, you should be able to install it from your favorite package manager (like apt or brew).

ImageMagick库非常流行,但是默认情况下通常不会安装它。 由于它的受欢迎程度,您应该可以从自己喜欢的软件包管理器(如aptbrew )安装它。

If you’re looking for more information on installation, you can checkout the Install-*.txt files available in the ImageMagick code repository.

如果要查找有关安装的更多信息,可以检出ImageMagick代码存储库中可用的Install-*.txt文件。

I’m going to assume you already have an image handy (JPG, PNG, etc.) to work with. If not, you should head over to multicolor.io and snag an image or two from there.

我假设您已经可以使用图像(JPG,PNG等)。 如果没有,您应该转到multicolor.io并从那里获取一两个图像。

调整为特定尺寸,忽略宽高比 (Resize to specific dimensions, ignoring the aspect ratio)

Resizing to specific dimensions without preserving the aspect ratio tends to not be a desired thing to do. Images end up looking distorted, unless the original aspect ratio was already pretty close to 1:1.

在不保留长宽比的情况下将尺寸调整为特定尺寸往往不是期望的事情。 除非原始宽高比已经非常接近1:1,否则图像最终看起来会失真。

That said, it’s still good to know how to resize this way, if nothing else, so we know what to avoid when punching in commands late at night.

就是说,知道如何以这种方式调整大小(如果没有其他内容)仍然很好,因此我们知道在深夜打Kong命令时应避免的事情。

To resize an image to specific dimensions, without maintaining the original image’s aspect ratio, include a bang ! at the end of the dimensions:

要将图像调整为特定尺寸而又不保持原始图像的宽高比,请加上bang ! 在尺寸结尾处:

$ convert original.png -resize 100x100! new.png

调整为特定尺寸并保持宽高比 (Resize to specific dimensions and keep aspect ratio)

To keep the aspect ratio, simply omit the bang:

要保持长宽比,只需省略爆炸:

$ convert original.png -resize 100x100 new.png

This won’t actually resize the image to the exact dimensions specified. What it will do is resize the image to fit within those dimensions.

这实际上不会将图像调整为指定的确切尺寸。 它将要做的是调整图像大小以适合这些尺寸。

The flip side, is that you can append ^ to the dimensions to tell ImageMagick that you’d like to resize the image to fill the dimensions, potentially overlapping on one side.

另一方面,您可以将^附加到尺寸上,以告诉ImageMagick您想要调整图像的大小以填充尺寸,这可能在一侧重叠。

One of the two dimensions (either width or height) will be scaled exactly, while the other will be scaled proportionately and may overlap:

这两个维度之一(宽度或高度)将精确缩放,而另一个维度将按比例缩放并可能重叠:

$ convert original.png -resize 100x100^ new.png

调整为特定尺寸并保持宽高比 (Resize to specific dimensions and keep the aspect ratio)

Sometimes you want to not only resize the image and fill the area, as well as keep it’s aspect ratio, but also crop it so there’s nothing overlapping. A good use case for this is user avatars. Obviously you don’t want to stretch or squash a user’s profile picture, so cropping it to a square is an okay solution:

有时,您不仅要调整图像的大小并填充区域,并保持其长宽比,还要对其进行裁切,以便没有重叠。 用户头像是一个很好的用例。 显然,您不想拉伸或挤压用户的个人资料图片,因此将其裁剪为正方形是一个好的解决方案:

$ convert original.png -resize 100x100^ -gravity center -extent 100x100 new.png

I say it’s only “okay” because users have grown more accustomed to being able to select the region of their uploaded image to serve as their avatar, so assuming they want the crop to the center of the image could cause a bit of a stir.

我说这只是“好”,因为用户已经越来越习惯于选择上传图像的区域作为化身,因此假设他们希望将作物裁剪到图像中心可能会引起一些轰动。

Also, depending on your needs, you can adjust the gravity to grab align differently, or even crop a size that doesn’t marry up exactly with the scaled dimensions.

另外,根据您的需要,您可以调整重力以进行不同的抓取,甚至可以裁切与缩放尺寸不完全吻合的尺寸。

调整文件大小 (Resizing files in place)

Thus far, we’ve been converting a file and saving it to a brand new file. While being a safer option, as it’s non-destructive to your original file, it’s not always a desired work flow.

到目前为止,我们一直在转换文件并将其保存为全新文件。 虽然这是一个更安全的选择,但它对原始文件无损,但并非总是理想的工作流程。

For the times you want to edit the file in place, you can swap the convert command with mogrify. The mogrify command accepts an input file that will be modified in place.

对于您要就地编辑文件的时间,可以将convert命令与mogrify交换。 mogrify命令接受将被修改的输入文件。

You are highly encouraged to make back ups of any images you are resizing in case you aren’t happy with the results of the resize.

强烈建议您备份要调整大小的所有图像,以防对调整结果不满意。

Here’s the “mogrifyed” versions of the previous commands:

这是先前命令的“ mogrify ed”版本:

$ mogrify -resize 100x100 original.png

调整多个文件的大小 (Resizing multiple files)

To take things a step further, if you wanted to be edit an entire directory of images and resize them all in the same way, you can pass mogrify a wild card string:

更进一步,如果您想编辑整个图像目录并以相同的方式调整它们的大小,则可以传递一个通配符字符串来mogrify

$ mogrify -resize 100x100! ./some/path/*.png
$ mogrify -resize 100x100^ -gravity center -extent 100x100 *.png

结论 (Conclusion)

Resizing images from the command-line is really just the tip of the iceberg. ImageMagick supports a ton of additional options that allow you to optimize images, play with the colors and even pixelate things.

从命令行调整图像大小实际上只是冰山一角。 ImageMagick支持大量其他选项,这些选项使您可以优化图像,发挥色彩甚至像素化效果。

You can even combine the power of command-line image editing and git to automatically scale and optimize your images as part of your pre-commit hook.

您甚至可以结合命令行图像编辑和git来自动缩放和优化图像,这是pre-commit挂钩的一部分。

翻译自: https://www.digitalocean.com/community/tutorials/workflow-resizing-images-with-imagemagick

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值