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

news/2024/7/6 1:11:17

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


http://www.niftyadmin.cn/n/3649587.html

相关文章

什么是华为认证?HCIA HCIP HCIE分别是什么认证体系?

一、华为公司简介 华为创立于1987年,是全球领先的ICT (信息与通信)基础设施和智能终端提供商,我们致力于把数字世界带入每个人、 每个家庭、每个组织,构建万物互联的智能世界。目前华为有18.8万员工,业务遍及170多个国…

Sync4j的Nokia手机实践结果

配置好Sync4j服务器后,我在Nokia手机上设置好同步,准备和这个远端服务器进行联系人的同步。下面的日志记录了两次失败的过程:第一次手机登录:由于Sync4j对设备要求事先在管理控制台上配置好,并添加规则,才能…

jarhoo是一个很棒的地方

jarhoo是一个很棒的根据类名找jar包的地方,。在写程序的时候,我反正是经常遇到某一个类声明不知道是哪一个jar包的,比如某一个开源包报告了java.lang.NoClassDefFoundError: javax/servlet/http/HttpSessionListener错误,搞得满世…

服务周期性工作内容_使服务工作者生命周期神秘化

服务周期性工作内容介绍 (Introduction) Service workers play a very vital role in Progressive Web Apps (PWA), as they are responsible for offline caching, push notifications, background sync etc. In this article, we’ll be demystifying the service worker lif…

Adobe Dreamweaver CC 2019 安装教程

一、DW简介 Adobe Dreamweaver,简称“DW”,中文名称 “梦想编织者”,最初为美国MACROMEDIA公司开发 ,2005年被Adobe公司收购。DW是集网页制作和管理网站于一身的所见即所得网页代码编辑器。利用对 HTML、CSS、JavaScript等内容的…

Json的知识点全解析,解析json数据的好工具大全

一、Json知识点 待续。。。。。。 二、Json解析工具 1、fastjson网址:http://fastjson.codeplex.com/ 教程博客:fastjson教程(http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html)2、网站:bejson网址:h…

CentOS系统:自动化运维工具Ansible的安装与配置

一、Ansible工具简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。 二、Ansible的安…

flutter 状态管理_管理Flutter中的状态

flutter 状态管理Most non-trivial apps will have some sort of state change going on and over time managing that complexity becomes increasingly difficult. Flutter apps are no different, but luckily for us, the Provider package is a perfect solution for most…