GraphicsMagick被称为图片处理的瑞士军刀,它的功能包括:
- Convert an image from one format to another (e.g. TIFF to JPEG)
- Resize, rotate, sharpen, color reduce, or add special effects to an image
- Create a montage of image thumbnails
- Create a transparent image suitable for use on the Web
- Compare two images
- Turn a group of images into a GIF animation sequence
- Create a composite image by combining several separate images
- Draw shapes or text on an image
- Decorate an image with a border or frame
- Describe the format and characteristics of an image
Linux下安装GraphicsMagick
安装libpng:
tar -zxvf libpng-1.6.14.tar.gz
cd libpng-1.6.14
./configure
make
make install安装jpeg-9a:
tar -zxvf jpegsrc.v9a.tar.gz
cd jpeg-9a
./configure
make
make install安装GraphicsMagick:
tar -zxvf GraphicsMagick-1.3.20.tar.gz
cd GraphicsMagick-1.3.20
./configure
make
make install
使用GraphicsMagick
输入gm可以得到该命令的使用说明,例如对以下490326的图片先缩放到原来的50%至245164再居中裁剪出164*164的正方形图片的命令是:
gm convert -quality 100 -resize 245x164 -crop 164x164+40+0 +profile “*” old.jpg new.jpg
其中,“-quality 100”表示图片质量,“-resize 245x164”表示将图片尺寸调整至245*164,“-crop 164x164+40+0”表示从坐标(40,0)开始裁剪出164*164的图片,+profile “*“表示不保留exif信息。
在Java中处理图片
在Java中可以通过im4java调用gm命令处理图片,上述对图片进行缩放再裁剪的操作在Java中可以通过以下代码实现:1
2
3
4
5
6
7
8
9IMOperation op = new IMOperation();
op.quality(100d);
op.resize(245, 164);
op.crop(164, 164, 40, 0);
op.p_profile("*");
op.addImage("old.jpg");
op.addImage("new.jpg");
ConvertCmd convert = new ConvertCmd(true);
convert.run(op);