红联Linux门户
Linux帮助

Ubuntu下使用shell脚本批量修改图片尺寸

发布时间:2017-04-09 10:43:38来源:sunzhongwei.com作者:大象笔记
Ubuntu 下需要安装 ImageMagick。执行
sudo apt-get install imagemagick
 
使用 shell 脚本遍历当前目录中的所有 png 图片,并逐一处理
set -e  # or use "set -o errexit" to quit on error.
set -x  # or use "set -o xtrace" to print the statement before you execute it.
FILES=*.png
for f in $FILES
do
echo "$f"
convert $f -resize 600x600 600_$f
done
 
环境
Ubuntu 16.10
ImageMagick 6.8.9
 
参考
How to Quickly Resize, Convert & Modify Images from the Linux Terminal
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 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 isn’t included in the default installations of Ubuntu and many other Linux distributions. To install it on Ubuntu, use the following command:
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 howtogeek.jpg
Ubuntu下使用shell脚本批量修改图片尺寸
You can also specify a compression level for JPEG images:
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.
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 example.png -resize 200×100 example.png
We’ve used the same file name here, so ImageMagick will overwrite the original file.
Ubuntu下使用shell脚本批量修改图片尺寸
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:
convert example.png -resize 200×100! 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:
convert example.png -resize 200 example.png
The following command will resize an image to a height of 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:
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.
Ubuntu下使用shell脚本批量修改图片尺寸
Applying Effects
ImageMagick can apply a variety of effects to an image. For example, the following command applies the “charcoal” effect to an image:
convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg
Ubuntu下使用shell脚本批量修改图片尺寸
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.
Ubuntu下使用shell脚本批量修改图片尺寸
The following command applies the “Implode” effect with a strength of 1:
convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg
Ubuntu下使用shell脚本批量修改图片尺寸
The implode effect makes it appear as if there’s a black hole at the center of the image.
Ubuntu下使用shell脚本批量修改图片尺寸
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 400×400 -rotate 180 -charcoal 4 -quality 95 howtogeek.jpg
Ubuntu下使用shell脚本批量修改图片尺寸
This is just the start of what you can do with ImageMagick. There are many more operations you can combine.
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.
for file in *.png; do convert $file -rotate 90 rotated-$file; done
Ubuntu下使用shell脚本批量修改图片尺寸
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.
 
本文永久更新地址:http://www.linuxdiyf.com/linux/29865.html