红联Linux门户
Linux帮助

Linux下批量缩小图片

发布时间:2007-06-20 00:57:39来源:红联作者:mendation
  周末出去玩,照了几张数码相片。上传到网上之前需要缩小一下,以前在Windows里都是用"光影魔术手",在Linux下如何完成这个工作呢?我开始是用GIMP一张的转,但太麻烦了,GIMP支持批处理脚本的,Google关键词"gimp-image-scale"可以找到一些这方面的脚本。不过更方便的是用Image Magick这个命令行图像处理软件。

  先用如下命令装上Image Magick:
  sudo apt-get install imagemagick

  缩图方法一:convert -resize 50%x50% photos\*.JPG small\

  这种方法转成的图片的文件名变成了数字,如果希望原文件名不变,可以用如下命令(实际上是一个四行合为一行的脚本)。

  缩图方法二:for img in `ls photos\*.JPG`; do convert -resize 50%x50% $img small-$img; done

  感觉要比如以前Windows下的软件方便多了。

  注:我之前曾象资料里用-sample参数,而非-resize。但前者可能不仅缩放了大小,还压缩了图像,所以我对缩小后的图片质量不太满意。用convert -h可以显示其所有参数,我后来选用了-resize。
文章评论

共有 1 条评论

  1. mendation 于 2007-06-20 00:58:04发表:

    mage MagicK 是一个强大的图象处理工具包。它提供了几个非常方便的命令行命令: display, animate,import, montage,mogrify,identify等,可以进行图象的显示,缩放,旋转,调色,加框,加注释等,还能制作GIF动画,图象索引,能自动生成图象.

    1. 制作索引图和动画

    !/bin/bash
    montage -bordercolor red -borderwidth 3 -label "%f" -tile 5x3 *.JPG montage.jpg
    mogrify -format gif *.JPG
    display montage.jpg
    animate *.JPG

    2. 缩放 convert -sample 80x40 input.jpg output.jpg #注意:缩放后图像保持原来的长宽比例 convert -sample 25%x25% input.jpg output.jpg
    3. 为当前目录的所有图像生成缩略图

    for img in `ls *.jpg`
    do
    convert -sample 25%x25% $img thumb-$img
    done

    4. 获取文件信息 libtiff

    tiffinfo filename.tiff
    pnginfo filename.png

    5. 可以使用 ImageMagick 的 identify

    identify -verbose sample.png
    identify -format "%wx%h" sample.png

    6. 旋转图像

    convert -rotate 90 input.jpg output.jpg

    7. 更改文件类型

    convert input.jpg output.png

    8. 为图像增加注释文字

    convert -font helvetica -fill white -pointsize 36 \
    -draw 'text 10,50 "Floriade 2002, Canberra, Australia"' \
    floriade.jpg comment.jpg
    convert -font fonts/1900805.ttf -fill white -pointsize 36 \
    -draw 'text 10,475 "stillhq.com"' \
    floriade.jpg stillhq.jpg

    9. 特殊效果

    convert -charcoal 2 input.jpg output.jpg #炭笔
    convert -colorize 255 input.jpg output.jpg #着色 可以指定三种颜色 red/green/blue
    convert -implode 4 input.jpg output.jpg #内爆效果
    convert -solarize 42 input.jpg output.jpg #曝光,模拟胶片曝光
    convert -spread 5 input.jpg output.jpg #随机移动,参数是位移大小

    10. 一次执行多个操作

    convert -sample 25%x25% -spread 4 -charcoal 4 input.jpg output.jpg