GraphicsMagick

GraphicsMagick与im4java图像处理技术详解
本文深入探讨了GraphicsMagick图像处理工具的特性与使用方法,包括其支持的图像格式、多线程处理能力以及与Java集成的示例。详细介绍了如何在Windows环境下安装GraphicsMagick并解决常见报错问题,通过Java代码演示了如何使用GraphicsMagick进行图片缩放、裁剪等操作。同时,提供了安装所需依赖库的方法,确保了图像处理功能的正常运行。
简介
        GraphicsMagick号称图像处理领域的瑞士军刀。 短小精悍的代码却提供了一个鲁棒、高效的工具和库集合,来处理图像的读取、写入和操作,支持超过88中图像格式,包括重要的DPX、GIF、JPEG、JPEG-2000、PNG、PDF、PNM和TIFF。
        通过使用OpenMP可是利用多线程进行图片处理,增强了通过扩展CPU提高处理能力。GraphicsMagick可以再绝大多数的平台上使用,Linux、Mac、Windows都没有问题。

        GraphicsMagick支持大图片的处理,并且已经做过GB级别的图像处理实验。GraphicsMagick能够动态的生成图片,特别适用于互联网的应用。可以用来处理调整尺寸、旋转、加亮、颜色调整、增加特效等方面。GaphicsMagick不仅支持命令行的模式,同时也支持C、C++、Perl、PHP、Tcl、Ruby等的调用。事实上,GraphicsMagick是从 ImageMagick 5.5.2 分支出来的,但是现在他变得更稳定和优秀,下面就是两个之间的一些比较。

安装
  • tar -xvf GraphicsMagick-1.3.12.tar
  • ./configure --without-prel --enable-shared --disable-openmp
  • make
  • make install

示例:GraphicsMagick+im4java压缩图片
Java代码 复制代码  收藏代码
  1. /**  
  2.      * 先缩放,后居中切割图片  
  3.      * @param srcPath 源图路径  
  4.      * @param desPath 目标图保存路径  
  5.      * @param rectw 待切割在宽度  
  6.      * @param recth 待切割在高度  
  7.      * @throws IM4JavaException   
  8.      * @throws InterruptedException   
  9.      * @throws IOException   
  10.      */  
  11. public static void cropImageCenter(String srcPath, String desPath, int rectw, int recth) throws IOException, InterruptedException, IM4JavaException{   
  12.     IMOperation op = new IMOperation();   
  13.            
  14.     op.addImage();   
  15.     op.resize(rectw, recth, '^').gravity("center").extent(rectw, recth);   
  16.     op.addImage();   
  17.   
  18.     ConvertCmd convert = new ConvertCmd(true);   
  19.     convert.run(op, srcPath, desPath);   
  20. }  
/**
	 * 先缩放,后居中切割图片
	 * @param srcPath 源图路径
	 * @param desPath 目标图保存路径
	 * @param rectw 待切割在宽度
	 * @param recth 待切割在高度
	 * @throws IM4JavaException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
public static void cropImageCenter(String srcPath, String desPath, int rectw, int recth) throws IOException, InterruptedException, IM4JavaException{
	IMOperation op = new IMOperation();
		
	op.addImage();
	op.resize(rectw, recth, '^').gravity("center").extent(rectw, recth);
	op.addImage();

	ConvertCmd convert = new ConvertCmd(true);
	convert.run(op, srcPath, desPath);
}

问题
        如果是windows环境安装则需要设置path环境配置,如D:\Program Files\GraphicsMagick-1.3.12-Q16;(此为GraphicsMagick的安装目录),各种版本官网下载

        报错信息"gm convert: No decode delegate for this image format (abc.jpg)”系图片库缺失所致
引用
http://qing.weibo.com/1838939461/6d9bfd4533000j33.html
./configure结果

GraphicsMagick is configured as follows. Please verify that this
configuration matches your expectations.

Host system type : x86_64-unknown-linux-gnu
Build system type : x86_64-unknown-linux-gnu

Option            Configure option              Configured value
-----------------------------------------------------------------
Shared libraries  --enable-shared=no            no
Static libraries  --enable-static=yes           yes
GNU ld            --with-gnu-ld=yes             yes
Quantum depth     --with-quantum-depth=8        8

Delegate Configuration:
BZLIB             --with-bzlib=yes              no
DPS               --with-dps=yes                no
FlashPIX          --with-fpx=no                 no
FreeType 2.0      --with-ttf=yes                no
Ghostscript       None                          gs (unknown)
Ghostscript fonts --with-gs-font-dir=default    none
Ghostscript lib   --with-gslib=no               no
JBIG              --with-jbig=yes               no
JPEG v1           --with-jpeg=yes               no (需安装 jpeg delegate library)
JPEG-2000         --with-jp2=yes                no
LCMS              --with-lcms=yes               no
Magick++          --with-magick-plus-plus=yes   yes
PERL              --with-perl=no                no
PNG               --with-png=yes                no(需安装 png delegate library)
TIFF              --with-tiff=yes               no
TRIO              --with-trio=yes               no
Windows fonts     --with-windows-font-dir=      none
WMF               --with-wmf=yes                no
X11               --with-x=                     no
XML               --with-xml=yes                no
ZLIB              --with-zlib=yes               yes
make
sudo make install
Delegate Library安装

JPEG delegate library

> gm convert 470f1bb8c5a98.jpg -resize 100x100 out.jpg
gm convert: No decode delegate for this image format (470f1bb8c5a98.jpg).
官方建议:This exception indicates that an external delegate library or its headers were not available when ImageMagick was built. To add support for the image format, download and install the requisite delegate library and its header files and reconfigure, rebuild, and reinstall ImageMagick. As an example, lets add support for the JPEG image format. First we install the JPEG RPMS:
原因是没有安装jpeg的包,这个可以在之前configure的结果中看出来,此时GraphicsMagick需要重新configure及build。
1)在GraphicsMagick官方网站提供的delegates下载页面中找到jpeg的包:libjpeg-6b.tar.gz。
2)安装jpeg delegate library. configure -> make -> sudo make install
3)重新安装GraphicsMagick (configure时会发现jpeg已安装)

PNG delegate library
gm convert: No decode delegate for this image format (ben3.png).
1)在sourceforge下载libpng-1.4.7.tar.gz  (libpng-1.5.2.tar.gz 版本装上后,GraphicsMagick 在make时会报一个‘去引用’的错,原因未知)
2)安装png delegate library. configure -> make -> sudo make install
3)重新安装GraphicsMagick (configure时会发现png已安装)
注: 由于之前安装过一次libpng-1.5.2,rm /usr/local/lib/libpng*,在重新安装了1.4.7后,仍有错误:gm: error while loading shared libraries: libpng14.so.14: cannot open shared object file: No such file or directory.
运行sudo ldconfig进行自动清理。

资料
IM4JAVA+GraphicsMagick处理网站图片: http://javantsky.iteye.com/blog/747807
关于GraphicsMagick+im4java的研究心得: http://yunduxiaocheng-gmail-com.iteye.com/blog/919457
GraphicsMagick+im4java图片处理方法: http://www.beiqiu.com/article/106.html
GraphicsMagick官网: http://www.graphicsmagick.org/index.html
GraphicsMagick官网delegates下载页面: ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/delegates/
### Linux 系统安装 GraphicsMagick #### 安装依赖库 在开始安装之前,确保系统中已经安装了必要的开发库和工具。对于基于 Red Hat 的系统(如 CentOS),可以使用以下命令安装所需的依赖: ```bash yum install -y gcc make libpng-devel libjpeg-devel libtiff-devel jasper-devel freetype-devel libtool-ltdl-devel ``` 对于基于 Debian 的系统(如 Ubuntu),可以使用以下命令: ```bash apt-get update apt-get install -y build-essential libpng-dev libjpeg-dev libtiff-dev libjasper-dev libfreetype6-dev libtool ``` #### 下载并解压 GraphicsMagick 源码包 接下来,下载 GraphicsMagick 的源代码包,并解压到当前目录: ```bash curl -o GraphicsMagick-1.3.36.tar.gz https://jaist.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.36/GraphicsMagick-1.3.36.tar.gz tar -zxvf GraphicsMagick-1.3.36.tar.gz cd GraphicsMagick-1.3.36/ ``` #### 配置安装选项 进入解压后的目录后,使用 `./configure` 命令来配置安装选项。这里以 `/app/GraphicsMagick-x` 作为安装路径为例: ```bash ./configure --prefix=/app/GraphicsMagick-x ``` 如果需要更详细的配置选项,可以指定更多的参数,例如: ```bash ./configure \ --prefix=/usr/local/GraphicsMagick-1.3.22 \ --exec-prefix=/usr/local/GraphicsMagick-1.3.22 \ --bindir=/usr/local/GraphicsMagick-1.3.22/bin \ --sbindir=/usr/local/GraphicsMagick-1.3.22/sbin \ --sysconfdir=/usr/local/GraphicsMagick-1.3.22/etc \ --datadir=/usr/local/GraphicsMagick-1.3.22/share \ --includedir=/usr/local/GraphicsMagick-1.3.22/include \ --libdir=/usr/local/GraphicsMagick-1.3.22/lib \ --libexecdir=/usr/local/GraphicsMagick-1.3.22/libexec \ --localstatedir=/usr/local/GraphicsMagick-1.3.22/var \ --sharedstatedir=/usr/local/GraphicsMagick-1.3.22/share/com \ --mandir=/usr/local/GraphicsMagick-1.3.22/share/man \ --infodir=/usr/local/GraphicsMagick-1.3.22/share/info \ --enable-libtool-verbose \ --with-included-ltdl \ --enable-shared \ --disable-static \ --with-modules \ --with-frozenpaths \ --without-perl \ --without-magick-plus-plus \ --with-quantum-depth=8 \ --enable-symbol-prefix ``` #### 编译并安装 配置完成后,使用 `make` 和 `make install` 命令进行编译和安装: ```bash make && make install ``` #### 配置环境变量 为了能够在任何地方调用 GraphicsMagick 的命令,需要将安装路径添加到系统的环境变量中。编辑 `/etc/profile` 文件,添加以下内容: ```bash export PATH=/app/GraphicsMagick-x/bin:$PATH ``` 保存文件后,使更改立即生效: ```bash source /etc/profile ``` ### Windows 系统安装 GraphicsMagick 对于 Windows 用户,可以直接从官方网站下载预编译的二进制文件。访问 [GraphicsMagick 官方网站](http://www.graphicsmagick.org/download.html),选择适合你系统的版本进行下载。 下载完成后,按照安装向导的提示完成安装过程。安装过程中可以选择自定义安装路径,并确保将 GraphicsMagick 的安装目录添加到系统环境变量中。 ### 测试安装 安装完成后,可以通过以下命令测试是否成功安装了 GraphicsMagick: ```bash gm convert -version ``` 如果输出显示了 GraphicsMagick 的版本信息,则表示安装成功。 ### 示例:图片缩放 使用 GraphicsMagick 进行图片缩放的操作非常简单。假设有一张名为 `test.png` 的图片,想要将其缩放到 50x50 像素,可以使用以下命令: ```bash gm convert /Users/user/Downloads/test.png -resize "50x50" resized_test.png ``` 这条命令会将 `test.png` 图片缩放到 50x50 像素,并将结果保存为 `resized_test.png`[^1]。 ### 相关问题 1. 如何在 Linux 系统上卸载 GraphicsMagick? 2. 在安装 GraphicsMagick 时遇到依赖问题怎么办? 3. 如何在 Windows 系统上配置 GraphicsMagick 的环境变量? 4. 使用 GraphicsMagick 进行图片处理时有哪些常用命令? 5. 如何验证 GraphicsMagick 是否正确安装?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值