发新话题
打印

批量处理图片大小的脚本

批量处理图片大小的脚本

最近爱摄影,电脑里很多照片。而且现在的相机拍出来都很大的尺寸,看看和传送都不方便。
一张张处理又麻烦,于是想写个程序来做。结果发现ImageMagick有个命令行的工具,就叫convert,于是就很方便的用shell写了个脚本。

注意:必须先安装ImageMagick sudo apt-get install imagemagick


resize_image.sh
复制内容到剪贴板
代码:
#!/bin/bash
# Used to resize the images

help_messge()
{
        echo "Usage: resize_image -d <dir> -w <width> -h <height>"
        echo "-d The directory contains the images"
        echo "-w The width after convert"
        echo "-h The height after convert"
        exit 1
}

if [ $# -lt 6 ]
then
        help_messge
fi

while getopts d:w:h: opt
do
        case "$opt" in
                d) dir="$OPTARG";;
                w) w="$OPTARG";;
                h) h="$OPTARG";;
        esac
done

if [ -d $dir ]
then
        cd $dir
        ls  *.jpg > filelist.$$
        for image in $(cat filelist.$$)
        do
                echo "Convert file $image"
                convert -resize "$w"x"$h" $dir/$image $dir/_$image
        done
        rm filelist.$$
        nautilus $dir
else
        echo "The directory $dir is not exist"
        exit 1
fi
当然还不是很严谨,但自己用用足够了,呵呵

[ 本帖最后由 bigapple 于 2007-7-3 22:51 编辑 ]
-bigapple-
风吹哪页读哪页
恩 好用的

最要用ruby写个  还可以加水印呢
邮箱 sanool at gmail.com  
php也可以做,有cli版本的,convert可以转换格式,改变大小,颜色数量等,功能很强大。
引用:
原帖由 sanool 于 2007-7-4 00:16 发表
恩 好用的

最要用ruby写个  还可以加水印呢
这个就交给你了,你正好在学习嘛
-bigapple-
风吹哪页读哪页
一般我只压缩图片
for f in `ls *jpg`; do convert -quality 70 ${f%.*}_c.jpg; done
Fluke's Blog
I'm a user.
结构很清晰的,很好。
/usr/bin/display
/usr/bin/animate
/usr/bin/montage
/usr/bin/mogrify
/usr/bin/identify
/usr/bin/convert
/usr/bin/conjure
/usr/bin/composite

[ 本帖最后由 黄富强 于 2007-7-7 18:49 编辑 ]

附件

Screenshot.png (229.74 KB)

2007-7-7 18:49

Screenshot.png

功能更强一些。

快想不起来这些了,不用linux桌面2年了...
http://luweinet.cublog.cn
不错!
引用:
原帖由 sanool 于 2007-7-4 00:16 发表
恩 好用的

最要用ruby写个  还可以加水印呢
/usr/bin/composite
composite(1)                                                                                    composite(1)

NAME
       composite -  overlaps one image over another.
这个就可以加水印
发新话题