使用命令缩放、压缩图片,工具包括: convert, mogrify, pngcrush, optipng, jpegoptim, trimage, gimp
- 参考
- Dave Taylor - Resizing Images with ImageMagick
- How to compress JPEG or PNG images in Linux using the terminal
- Optimize and Compress JPEG or PNG Images in Linux Command line
- How to Convert Images Using Linux
- Best Image Compression Apps for Linux
- How to Optimize and Compress JPEG or PNG Images in Linux Commandline
- Reduce File Size of Images in Linux – CLI and GUI methods
- 包括: convert, mogrify, pngcrush, jpegoptim, trimage
- Command-line Basics: Resizing Images with ImageMagick
技巧方法
使用 imagemagick 缩放图片
手机拍摄的高分辨率照片,可以用这个方法快速缩放
1 2 3 | sudo pacman -S imagemagick convert origin.jpg --resize 20% -quality 70 result.jpg |
identify 查看图片信息
1 | identify your-image.jpg |
提取图片长宽的脚本
- scaledown.sh
1 2 3 4 5 6 7 8 9 10 11 | #!/bin/sh identify=/usr/bin/identify scale=$1 image=$2 # needs input validation code height=$($identify $image | cut -d\ -f3 | cut -dx -f1) width=$($identify $image | cut -d\ -f3 | cut -dx -f2) newwidth="$(echo $width \* $scale | bc | cut -d. -f1)" newheight="$(echo $height \* $scale | bc | cut -d. -f1)" echo "<img src=$image height=$newheight width=$newwidth>" exit 0 |
- 使用脚本
1 2 | $ scaledown.sh 0.5 pvp.jpg <img src=pvp.jpg height=152 width=485> |
convert , identify 相互结合的脚本,缩放并自动生成包含尺寸的文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #!/bin/sh convert=/usr/bin/convert identify=/usr/bin/identify resize=$1 source=$2 if [ -z "$resize" -o -z "$source" ] ; then echo "Usage: $0 resize sourcefile"; ;exit 1 fi if [ ! -r $source ] ; then echo "Error: can't read source file $source" ; exit 1 fi # let's grab the filename suffix filetype=$(echo $source | rev | cut -d. -f1 | rev) tempfile="resize.$filetype" # temp file name # create the newly sized temp version of the image $convert $source -resize $resize $tempfile # figure out geometry, the assemble new filename geometry=$($identify $tempfile | cut -d\ -f3 ) newfilebase=$(echo $source | sed "s/$filetype//") newfilename=$newfilebase$geometry.$filetype # rename temp file and we're done mv $tempfile $newfilename echo \*\* resized $source to new size $resize. result = $newfilename exit 0 |
- 示例
1 2 | sh resize.sh 50% pvp.jpg ** resized pvp.jpg to new size 50%. result = pvp.485x153.jpg |
使用 jpegoptim 压缩 jpeg
1 2 | sudo apt-get install jpegoptim jpegoptim your-target.jpg |
使用 optipng 压缩 png
1 2 | sudo pacman -S optipng optipng gfg.png |
图形化工具 Trimage
没试过,mark 下