ImageMagic---convert(格式转换)

ImageMagic---convert(图片格式转换)

官方帮助文档:
http://http://www.imagemagick.org/Usage/basics/

优秀分支GM
http://www.graphicsmagick.org/

1.格式转换,支持JPG, BMP, PCX, GIF, PNG, TIFF, XPM和XWD等类型,基本上gimp支持的格式ImageMagick都支持
convert   from.jpg  to.png   #将jpeg转成png
convert  from.gif    to.bmp  #将gif转换成bmp

下面提供批量转格式脚本,供大家参考
#####################################################
#!/bin/bash
#To batch convert pictures
#Made by liujun, liujun_live@msn.com, 20140820
#####################################################
# Source function library.
. /etc/init.d/functions
#Export PATH
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#Define variables
Format_From=jpg           #原格式,png|bmp|gif,不要加.
Format_To=gif            #目标格式,png|bmp|gif,不要加.
Directory_From=/tmp       #原目录绝对路径
Directory_To=/tmp/test         #目标目录绝对路径
Size=            #指定目标尺寸大小1024x768
Pictures=$(ls $Directory_From/*.$Format_From)  #所有待转的图片

#Create the target directory
if [ ! -d "$Directory_To" ];then
    mkdir -p $Directory_To
fi


for picture in $Pictures
do
    BaseName=$(basename $picture)
    if [ "$Size" == "" ];then
    convert $Directory_From/$BaseName $Directory_To/${BaseName%.*}.$Format_To
    echo -e "e[31;1m$Directory_From/$BaseNamee[0m e[34;1m======>e[0m e[32;1m$Directory_To/${BaseName%.*}.$Format_Toe[0m"
    else
        convert -resize $Size $Directory_From/$BaseName $Directory_To/${BaseName%.*}.$Format_To
        echo -e "e[31;1m$Directory_From/$BaseNamee[0m e[34;1m======>e[0m e[35;1m$Sizee[0m e[32;1m$Directory_To/${BaseName%.*}.$Format_Toe[0m"
    fi

done
echo -e " "
echo -e "Converting e[32;1msucessfully!e[0m"
exit 0

################################################

交互式脚本1:
#!/bin/bash
echo -e "请按下列格式输入图片所在\033[31;1m目录\e[0m \e[32;1m源格式\033[0m \e[33;1m目标格式\e[0m \e[34;1m分辨率\e[0m \e[35;1m色彩深度\e[0m"
echo ""
echo "/home/test png xpm 640x480 16"
echo ""
read -p "You input:" input

dir=$(echo $input|gawk '{print $1}')
sf=$(echo $input|gawk '{print $2}')
of=$(echo $input|gawk '{print $3}')
ge=$(echo $input|gawk '{print $4}')
co=$(echo $input|gawk '{print $5}')

echo "Please wait a moment"
cd $dir
for i in $(ls *.$sf)
    do
        convert $i -geometry $ge -colors $co ${i%.*}.$of|xargs gzip -f
    done
echo "Done"


交互式脚本2:
#!/bin/bash
read -p "Directory:" dir
read -p "source format:(png or bmp ...)" sf
read -p "object format:(xpm or ...)" of
read -p "geometry:(800x600...)" go
read -p "colors:(16 ...)" co
for i in $(ls *.$sf $dir)
    do
        convert $i -geometry  $go -colors $co  ${i%.*}.$of
    done
echo "Done"


以下是官方提供的实现方式,供参考之用
# Use find to substitute filenames into a 'convert' command.
  
# This also provides the ability to recurse though directories by removing
# the -prune option, as well as doing other file checks (like image type,
# or the disk space used by an image).
find * -prune -name '*.jpg' -exec convert '{}' -thumbnail 200x90 thumbnails/'{}'.gif ;

# Use xargs -- with a shell wrapper to put the argument into a variable
# This can be combined with either "find" or "ls" to list filenames.
ls *.jpg | xargs -n1 sh -c 'convert $0 -thumbnail 200x90 thumbnails/$0.gif'

# An alternative method on linux (rather than plain unix)
# This does not need a shell to handle the argument.
ls *.jpg | xargs -r -I FILE convert FILE -thumbnail 200x90 FILE_thumb.gif

2.修改图像的尺寸
convert  -resize  1024x768   from.jpg   to.gif    #将图像的像素改为1024x768并将格式转为gif,注意1024与768之间是小写字母x
convert  -sample  50%x50%  from.jpg   to.png   #将图像的缩减为原来的50%x50%

3.图像旋转
convert  -rotate 270  from.jpg   to.bmp   #将图像顺时针旋转270度
convert  -transpose   from.jpg   to.bmp   #镜像图像后顺时针旋转90度
convert  -transverse   from.jpg   to.bmp   #镜像图像后顺时针旋转270度
-draw     #可以在图像里面添加文字
convert  -fill  black  -pointsize  60  -font  helvetica  -draw  ' text 10,80  "Hello, World!" ‘   from.jpg  to.png
在图像的10,80 位置采用60磅的全黑Helvetica字体写上 Hello, World!


至于高阶用法,如:裁剪、淡化、抖动、炭化、加边框、圆角等等一系列操作,可以参考官方帮助文档


linux

原文地址:https://www.cnblogs.com/lixuebin/p/10814519.html