linux映像_如何从Linux终端快速调整大小,转换和修改映像

本文介绍了如何使用ImageMagick命令行工具在Linux终端进行图像处理,包括安装、格式转换、图像大小调整、旋转、应用效果、批处理等操作。ImageMagick可以方便地对单个或多个图像进行快速修改,并能集成到Bash脚本中。
摘要由CSDN通过智能技术生成
linux映像

linux映像

header imagemagick

ImageMagick is a suite of command-line utilities for modifying and working with images. ImageMagick can quickly perform operations on an image from a terminal, perform batch processing of many images, or be integrated into a bash script.

ImageMagick是一套用于修改和使用图像的命令行实用程序。 ImageMagick可以快速从终端对图像执行操作,对许多图像进行批处理,或者集成到bash脚本中。

ImageMagick can perform a wide variety of operations. This guide will introduce you to ImageMagick’s syntax and basic operations and show you how to combine operations and perform batch processing of many images.

ImageMagick可以执行多种操作。 本指南将向您介绍ImageMagick的语法和基本操作,并向您展示如何组合操作并执行许多图像的批处理。

安装 (Installation)

ImageMagick isn’t included in the default installations of Ubuntu and many other Linux distributions. To install it on Ubuntu, use the following command:

Ubuntu和许多其他Linux发行版的默认安装中未包含ImageMagick。 要将其安装在Ubuntu上,请使用以下命令:

sudo apt-get install imagemagick

在格式之间转换 (Converting Between Formats)

The convert command takes an image, performs actions on it, and saves the image with the file name you specify. One of the most basic things you can do with it is converting images between formats. The following command takes a PNG file named “howtogeek.png” in the current directory and creates a JPEG image from it:

convert命令获取图像,对其执行操作,然后使用您指定的文件名保存该图像。 您可以执行的最基本的操作之一就是在格式之间转换图像。 以下命令在当前目录中获取一个名为“ howtogeek.png”的PNG文件,并从中创建一个JPEG图像:

convert howtogeek.png howtogeek.jpg
convert formats

You can also specify a compression level for JPEG images:

您还可以为JPEG图像指定压缩级别:

convert howtogeek.png -quality 95 howtogeek.jpg

The number must be between 1 and 100. ImageMagick uses the quality level of the input image, if possible. If not, ImageMagick defaults to 92.

该数字必须在1到100之间。ImageMagick尽可能使用输入图像的质量等级。 如果不是,则ImageMagick默认为92。

调整图像大小 (Resizing Images)

The convert command can also quickly resize an image. The following command asks ImageMagick to resize an image to 200 pixels in width and 100 pixels in height:

convert命令还可以快速调整图像大小。 以下命令要求ImageMagick将图像调整为宽度200像素和高度100像素:

convert example.png -resize 200x100 example.png

We’ve used the same file name here, so ImageMagick will overwrite the original file.

我们在这里使用了相同的文件名,因此ImageMagick将覆盖原始文件。

resize

ImageMagick will try to preserve the aspect ratio if you use this command. It will alter the image to fit within a 200×100 area, but the image may not be exactly 200×100. If you want to force the image to become a specific size – even if it messes up the aspect ratio – add an exclamation point to the dimensions:

如果使用此命令,ImageMagick将尝试保留宽高比。 它将更改图像以适合200×100的区域,但是图像可能不完全是200×100。 如果要强制将图像变成特定的大小(即使它弄乱了宽高比),请在尺寸上添加一个感叹号:

convert example.png -resize 200x100! example.png

You can also specify a specific width or height and ImageMagick will resize the image to that width or height while preserving the aspect ratio. The following command will resize an image to a width of 200:

您还可以指定特定的宽度或高度,ImageMagick会在保留纵横比的同时将图像调整为该宽度或高度。 以下命令会将图像大小调整为200的宽度:

convert example.png -resize 200 example.png

The following command will resize an image to a height of 100:

以下命令将图像大小调整为100的高度:

convert example.png -resize x100 example.png

旋转影像 (Rotating an Image)

ImageMagick can quickly rotate an image. The following command takes an image named howtogeek.jpg, rotates it by 90 degrees and saves the rotated image as howtogeek-rotated.jpg:

ImageMagick可以快速旋转图像。 以下命令获取名为howtogeek.jpg的图像,将其旋转90度并将旋转后的图像另存为howtogeek-rotated.jpg:

convert howtogeek.jpg -rotate 90 howtogeek-rotated.jpg

If you specified the same file name, ImageMagick would save the rotated image over the original image file.

如果指定了相同的文件名,则ImageMagick会将旋转后的图像保存在原始图像文件上。

rotate

应用效果 (Applying Effects)

ImageMagick can apply a variety of effects to an image. For example, the following command applies the “charcoal” effect to an image:

ImageMagick可以对图像应用各种效果。 例如,以下命令将“木炭”效果应用于图像:

convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg
charcoal

The charcoal command applies an artistic “charcoal” style effect to an image – the 2 in the command lets you control the strength of the effect.

木炭命令将艺术性的“木炭”样式效果应用于图像–命令中的2可让您控制效果的强度。

howtogeek-charcoal

The following command applies the “Implode” effect with a strength of 1:

以下命令应用强度为1的“放大”效果:

convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg
implode

The implode effect makes it appear as if there’s a black hole at the center of the image.

爆裂效果使其看起来好像图像中心有黑洞。

howtogeek-imploded

合并作业 (Combining Operations)

All these operations can be combined. With a single command, you could resize an image, rotate it, apply an effect, and convert it to another format:

所有这些操作可以合并。 使用单个命令,您可以调整图像大小,旋转图像,应用效果并将其转换为另一种格式:

convert howtogeek.png -resize 400x400 -rotate 180 -charcoal 4 -quality 95 howtogeek.jpg
howtogeek-complex

This is just the start of what you can do with ImageMagick. There are many more operations you can combine.

这只是您使用ImageMagick可以做的开始。 您可以组合更多的操作。

批量处理 (Batch Processing)

You can take advantage of Bash to quickly do batch processing of many images. For example, the following command would take all PNG files in the current directory, rotate them, and save a new copy of each with “rotated-” added to the beginning of each file name.

您可以利用Bash快速处理许多图像。 例如,以下命令将获取当前目录中的所有PNG文件,对其进行旋转,然后使用每个文件名的开头添加“ rotated-”的形式保存每个文件的新副本。

for file in *.png; do convert $file -rotate 90 rotated-$file; done
batch processing

You can easily modify this command to perform other actions. You can also integrate batch processing commands into a Bash shell script to automate image-processing operations.

您可以轻松地修改此命令以执行其他操作。 您还可以将批处理命令集成到Bash Shell脚本中,以自动执行图像处理操作。



Any article on ImageMagick will omit a lot of what you can do with it – there are just too many options and commands. If you’re interested in doing more with ImageMagick, check out the official documentation on the ImageMagick website for a much more in-depth look at ImageMagick.

关于ImageMagick的任何文章都将忽略您可以做的很多事情-太多的选项和命令。 如果您有兴趣使用ImageMagick进行更多操作,请查看ImageMagick网站上的官方文档,以更深入地了解ImageMagick。

翻译自: https://www.howtogeek.com/109369/how-to-quickly-resize-convert-modify-images-from-the-linux-terminal/

linux映像

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值