图像魔法棒:ImageMagick实用脚本指南
1. 水印添加脚本
在处理图像时,为图片添加水印是一项常见的需求。下面是一个使用 ImageMagick 实现水印添加的脚本示例:
newfilename="$prefix+wm.$suffix"
x composite -dissolve 75% -gravity south $wmfile "$1" "$newfilename"
echo "Created new watermarked image file $newfilename."
exit 0
这个脚本虽然看起来有些复杂,但它的核心功能就是为图片添加水印。具体步骤如下:
1.
获取图像尺寸
:确保水印覆盖层的尺寸与图像尺寸完全一致,否则可能会出现问题。
2.
生成新文件名
:在原文件名的基础上添加“+wm”后缀。这里使用了
rev
和
cut
命令来处理文件名,以确保能正确获取后缀。
3.
合成水印
:使用
composite
工具将水印与图像合成。可以通过调整
-dissolve
的值来改变水印的透明度。
运行脚本时,需要提供两个参数:要添加水印的图像文件名和水印文本。如果水印包含多个单词,需要用引号将整个短语括起来。例如:
$ watermark test.png "(C) 2016 by Dave Taylor"
Created new watermarked image file test+wm.png.
如果在运行过程中遇到“unable to read font”错误,可能是缺少 Ghostscript 软件套件。在 OS X 系统上,可以使用以下命令安装:
$ brew install ghostscript
为了进一步优化这个脚本,可以考虑将水印字体大小与图像大小关联起来,让用户可以指定水印字体。
2. 图像加框脚本
有时候,我们希望为图像添加边框或精美的框架,ImageMagick 的
convert
工具可以实现这一功能。以下是一个实现图像加框的脚本:
#!/bin/bash
# frameit--Makes it easy to add a graphical frame around
# an image file, using ImageMagick
usage()
{
cat << EOF
Usage: $(basename $0) -b border -c color imagename
or $(basename $0) -f frame -m color imagename
In the first case, specify border parameters as size x size or
percentage x percentage followed by the color desired for the
border (RGB or color name).
In the second instance, specify the frame size and offset,
followed by the matte color.
EXAMPLE USAGE:
$(basename $0) -b 15x15 -c black imagename
$(basename $0) -b 10%x10% -c gray imagename
$(basename $0) -f 10x10+10+0 imagename
$(basename $0) -f 6x6+2+2 -m tomato imagename
EOF
exit 1
}
#### MAIN CODE BLOCK
# Most of this is parsing starting arguments!
while getopts "b:c:f:m:" opt; do
case $opt in
b ) border="$OPTARG"; ;;
c ) bordercolor="$OPTARG"; ;;
f ) frame="$OPTARG"; ;;
m ) mattecolor="$OPTARG"; ;;
? ) usage; ;;
esac
done
shift $(($OPTIND - 1)) # Eat all the parsed arguments.
if [ $# -eq 0 ] ; then # No images specified?
usage
fi
# Did we specify a border and a frame?
if [ ! -z "$bordercolor" -a ! -z "$mattecolor" ] ; then
echo "$0: You can't specify a color and matte color simultaneously." >&2
exit 1
fi
if [ ! -z "$frame" -a ! -z "$border" ] ; then
echo "$0: You can't specify a border and frame simultaneously." >&2
exit 1
fi
if [ ! -z "$border" ] ; then
args="-bordercolor $bordercolor -border $border"
else
args="-mattecolor $mattecolor -frame $frame"
fi
for name
do
suffix="$(echo $name | rev | cut -d. -f1 | rev)"
prefix="$(echo $name | rev | cut -d. -f2- | rev)"
newname="$prefix+f.$suffix"
echo "Adding a frame to image $name, saving as $newname"
convert $name $args $newname
done
exit 0
这个脚本的工作流程可以用下面的 mermaid 流程图表示:
graph TD;
A[开始] --> B[解析参数];
B --> C{是否指定图像};
C -- 否 --> D[显示用法并退出];
C -- 是 --> E{是否同时指定边框颜色和遮罩颜色};
E -- 是 --> F[报错并退出];
E -- 否 --> G{是否同时指定边框和框架};
G -- 是 --> H[报错并退出];
G -- 否 --> I{是否指定边框};
I -- 是 --> J[设置边框参数];
I -- 否 --> K[设置框架参数];
J --> L[遍历图像文件];
K --> L;
L --> M[生成新文件名];
M --> N[添加框架并保存];
N --> O[结束];
运行脚本时,需要指定框架类型(
-border
或
-frame
)、相应的 ImageMagick 几何值、边框或遮罩部分的颜色以及输入的图像文件名。例如:
$ frameit -f 15%x15%+10+10 -m black abandoned-train.png
Adding a frame to image abandoned-train.png, saving as abandoned-train+f.png
为了提高脚本的健壮性,可以添加额外的错误测试,避免因忘记参数而出现的错误,同时处理包含空格的文件名。
3. 图像缩略图生成脚本
在网页或邮件中使用过大的图像会浪费带宽和计算机资源,因此生成图像缩略图是很有必要的。以下是一个生成图像缩略图的脚本:
#!/bin/bash
# thumbnails--Creates thumbnail images for the graphics file specified,
# matching exact dimensions or not-to-exceed dimensions
convargs="-unsharp 0x.5 -resize"
count=0; exact=""; fit=""
usage()
{
echo "Usage: $0 (-e|-f) thumbnail-size image [image] [image]" >&2
echo "-e resize to exact dimensions, ignoring original proportions" >&2
echo "-f fit image into specified dimensions, retaining proportion" >&2
echo "-s strip EXIF information (make ready for web use)" >&2
echo " please use WIDTHxHEIGHT for requested size (e.g., 100x100)"
exit 1
}
#############
## BEGIN MAIN
if [ $# -eq 0 ] ; then
usage
fi
while getopts "e:f:s" opt; do
case $opt in
e ) exact="$OPTARG"; ;;
f ) fit="$OPTARG"; ;;
s ) strip="-strip"; ;;
? ) usage; ;;
esac
done
shift $(($OPTIND - 1)) # Eat all the parsed arguments.
rwidth="$(echo $exact $fit | cut -dx -f1)" # Requested width
rheight="$(echo $exact $fit | cut -dx -f2)" # Requested height
for image
do
width="$(identify -format "%w" "$image")"
height="$(identify -format "%h" "$image")"
# Building thumbnail for image=$image, width=$width, and height=$height
if [ $width -le $rwidth -a $height -le $rheight ] ; then
echo "Image $image is already smaller than requested dimensions. Skipped."
else
# Build new filename.
suffix="$(echo $image | rev | cut -d. -f1 | rev)"
prefix="$(echo $image | rev | cut -d. -f2- | rev)"
newname="$prefix-thumb.$suffix"
# Add the "!" suffix to ignore proportions as needed.
if [ -z "$fit" ] ; then
size="$exact!"
echo "Creating ${rwidth}x${rheight} (exact size) thumb for file $image"
else
size="$fit"
echo "Creating ${rwidth}x${rheight} (max size) thumb for file $image"
fi
convert "$image" $strip $convargs "$size" "$newname"
fi
count=$(( $count + 1 ))
done
if [ $count -eq 0 ] ; then
echo "Warning: no images found to process."
fi
exit 0
这个脚本使用了
-unsharp
和
-strip
参数,前者用于防止缩略图模糊,后者用于去除 EXIF 信息。运行脚本时,可以指定缩略图的尺寸和是否保留原始比例。例如:
$ thumbnails -s -e 300x300 hawaii.png
Creating 300x300 (exact size) thumb for file hawaii.png
$ thumbnails -f 300x300 hawaii.png
Creating 300x300 (max size) thumb for file hawaii.png
为了增强脚本的功能,可以考虑根据多个尺寸范围生成多种缩略图。
4. GPS 地理定位信息解析脚本
现在很多照片都包含 GPS 地理定位信息,但 ImageMagick 提取的信息格式不太直观。以下是一个将 EXIF 信息转换为 Google Maps 或 Bing Maps 可识别格式的脚本:
#!/bin/bash
# geoloc--For images that have GPS information, converts that data into
# a string that can be fed to Google Maps or Bing Maps
tempfile="/tmp/geoloc.$$"
trap "$(which rm) -f $tempfile" 0 1 15
if [ $# -eq 0 ] ; then
echo "Usage: $(basename $0) image" >&2
exit 1
fi
for filename
do
identify -format "%[EXIF:*]" "$filename" | grep GPSL > $tempfile
latdeg=$(head -1 $tempfile | cut -d, -f1 | cut -d= -f2)
latdeg=$(scriptbc -p 0 $latdeg)
latmin=$(head -1 $tempfile | cut -d, -f2)
latmin=$(scriptbc -p 0 $latmin)
latsec=$(head -1 $tempfile | cut -d, -f3)
latsec=$(scriptbc $latsec)
latorientation=$(sed -n '2p' $tempfile | cut -d= -f2)
longdeg=$(sed -n '3p' $tempfile | cut -d, -f1 | cut -d= -f2)
longdeg=$(scriptbc -p 0 $longdeg)
longmin=$(sed -n '3p' $tempfile | cut -d, -f2)
longmin=$(scriptbc -p 0 $longmin)
longsec=$(sed -n '3p' $tempfile | cut -d, -f3)
longsec=$(scriptbc $longsec)
longorientation=$(sed -n '4p' $tempfile | cut -d= -f2)
echon "Coords: $latdeg ${latmin}' ${latsec}\" $latorientation, "
echo "$longdeg ${longmin}' ${longsec}\" $longorientation"
done
exit 0
这个脚本的工作流程如下:
1. 使用
identify
工具提取 EXIF 信息,并使用
grep
过滤出 GPS 相关信息。
2. 提取纬度和经度的度、分、秒信息,并使用
scriptbc
进行数学计算。
3. 重新组合信息,输出符合标准格式的经纬度信息。
运行脚本时,提供包含 GPS 信息的图像文件名即可:
$ geoloc parking-lot-with-geotags.jpg
Coords: 40 3' 19.73" N, 103 12' 3.72" W
为了提高脚本的健壮性,需要处理输入照片不包含 EXIF 信息的情况。
5. 日期计算差异
在进行日期计算时,Unix 系统(如 OS X)和基于 GNU 的 Linux 系统存在差异。GNU 版本的
date
工具功能更强大。如果在 OS X 系统上使用
date --version
命令报错,可以使用
brew
安装
coreutils
来获取 GNU
date
工具:
$ brew install coreutils
通过以上这些脚本,我们可以利用 ImageMagick 完成图像水印添加、加框、缩略图生成和 GPS 信息解析等常见任务,同时也了解了不同系统在日期计算方面的差异。这些脚本不仅提高了工作效率,还为我们处理图像和日期提供了便利。在实际使用中,可以根据需要对脚本进行修改和扩展,以满足更多的需求。
图像魔法棒:ImageMagick实用脚本指南
6. 脚本使用总结与对比
为了更清晰地了解各个脚本的使用方法和特点,下面通过表格进行总结对比:
| 脚本名称 | 功能 | 参数说明 | 示例 |
| — | — | — | — |
| 水印添加脚本 | 为图像添加水印 | 要添加水印的图像文件名、水印文本 |
$ watermark test.png "(C) 2016 by Dave Taylor"
|
| 图像加框脚本 | 为图像添加边框或框架 |
-b
指定边框参数、
-c
指定边框颜色、
-f
指定框架参数、
-m
指定遮罩颜色、图像文件名 |
$ frameit -f 15%x15%+10+10 -m black abandoned-train.png
|
| 图像缩略图生成脚本 | 生成图像缩略图 |
-e
指定精确尺寸、
-f
指定最大尺寸、
-s
去除 EXIF 信息、缩略图尺寸、图像文件名 |
$ thumbnails -s -e 300x300 hawaii.png
|
| GPS 地理定位信息解析脚本 | 解析图像的 GPS 地理定位信息 | 包含 GPS 信息的图像文件名 |
$ geoloc parking-lot-with-geotags.jpg
|
从表格中可以看出,每个脚本都有其特定的功能和参数,在使用时需要根据具体需求进行选择。
7. 脚本优化建议
虽然这些脚本已经能够完成基本的任务,但为了提高脚本的健壮性和功能,还可以进行以下优化:
-
水印添加脚本
:
- 关联水印字体大小与图像大小,例如可以根据图像宽度动态调整字体大小。
- 允许用户指定水印字体,通过添加一个新的参数来实现。
-
图像加框脚本
:
- 添加额外的错误测试,检查用户输入的参数是否合法,例如检查边框尺寸和颜色是否符合要求。
- 处理包含空格的文件名,可以使用引号或转义字符来避免问题。
-
图像缩略图生成脚本
:
- 根据多个尺寸范围生成多种缩略图,例如可以接受多个尺寸参数,循环生成不同尺寸的缩略图。
- 增加对不同图像格式的支持,确保脚本可以处理各种常见的图像文件。
-
GPS 地理定位信息解析脚本
:
- 处理输入照片不包含 EXIF 信息的情况,当检测到没有 GPS 信息时,输出友好的提示信息。
- 优化信息提取和计算的效率,减少脚本的执行时间。
8. ImageMagick 工具拓展应用思路
除了上述脚本所实现的功能,ImageMagick 还有很多其他的应用场景,以下是一些拓展应用思路:
-
图像拼接
:可以将多个图像拼接成一个大图,例如制作全景图或拼贴画。可以使用
montage
工具来实现,示例代码如下:
montage image1.jpg image2.jpg image3.jpg -tile 3x1 -geometry +5+5 output.jpg
这个命令将三个图像水平拼接成一个大图,每个图像之间有 5 像素的间距。
-
图像特效处理
:可以为图像添加各种特效,如模糊、锐化、色彩调整等。例如,使用
convert
工具添加高斯模糊特效:
convert input.jpg -gaussian-blur 0x5 output.jpg
这个命令将输入图像进行高斯模糊处理,模糊半径为 5 像素。
-
图像格式转换
:可以将图像从一种格式转换为另一种格式,例如将 JPEG 转换为 PNG。使用
convert
工具可以轻松实现:
convert input.jpg output.png
9. 日期计算在实际场景中的应用
在实际开发中,日期计算有很多应用场景,例如计算两个日期之间的天数、判断某一年是否为闰年等。下面是一个计算两个日期之间天数的示例脚本:
#!/bin/bash
date1=$1
date2=$2
delta=$(($(date +%s -d "$date2") - $(date +%s -d "$date1")))
days=$((delta / 86400))
echo "Days between $date1 and $date2: $days"
运行这个脚本时,需要提供两个日期作为参数,例如:
$ ./date_diff.sh 2023-01-01 2023-01-10
Days between 2023-01-01 and 2023-01-10: 9
10. 总结与展望
通过使用 ImageMagick 相关的脚本,我们可以方便地完成图像水印添加、加框、缩略图生成和 GPS 信息解析等任务,同时也了解了不同系统在日期计算方面的差异。这些脚本不仅提高了工作效率,还为我们处理图像和日期提供了便利。
在未来的开发中,我们可以进一步拓展 ImageMagick 的应用,结合其他工具和技术,实现更复杂的图像处理任务。例如,将图像处理与机器学习相结合,实现图像识别、分类等功能。同时,对于日期计算,也可以开发更完善的工具,满足不同场景下的需求。
总之,掌握 ImageMagick 和日期计算的相关知识和技巧,将有助于我们更好地处理图像和日期数据,为开发工作带来更多的便利和可能性。
以下是一个 mermaid 流程图,展示了整个图像处理和日期计算的应用流程:
graph LR;
A[选择任务] --> B{图像处理};
A --> C{日期计算};
B --> D[水印添加];
B --> E[图像加框];
B --> F[缩略图生成];
B --> G[GPS 信息解析];
C --> H[日期差值计算];
C --> I[闰年判断];
D --> J[输出结果];
E --> J;
F --> J;
G --> J;
H --> K[输出结果];
I --> K;
这个流程图展示了用户可以选择进行图像处理或日期计算,然后根据具体需求选择相应的子任务,最后输出处理结果。
超级会员免费看
8155

被折叠的 条评论
为什么被折叠?



