简介:
GraphicsMagick 号称图像处理领域的瑞士军刀。 短小精悍的代码却提供了一个鲁棒、高效的工具和库集合,来处理图像的读取、写入和操作,支持超过 88 种图像格式,包括重要的 DPX 、GIF 、JPEG 、JPEG-2000 、PNG 、PDF 、PNM 和 TIFF 。
- 通过使用 OpenMP 可以利用多线程进行图片处理,增强了处理能力;
- GraphicsMagick 可以在绝大多数的平台上使用,Linux 、Mac 和 Windows 都没有问题;
- GraphicsMagick 支持大图片的处理,并且已经做过 GB 级别的图像处理实验;
- GraphicsMagick 能够动态的生成图片,特别适用于互联网的应用,还可以用来处理调整尺寸、旋转、加亮、颜色调整、增加特效等方面;
- GaphicsMagick 不仅支持命令行的模式,同时也支持 C 、C++ 、Java 、Perl 、PHP 、Tcl 和 Ruby 等的调用;
- 事实上,GraphicsMagick 是从 ImageMagick 5.5.2 分支出来的,但是现在他变得更稳定和优秀。
总而言之在命令行界面的 Linux 装上它,然后在 Java 里调用 GraphicsMagick 来处理图片,是非常好用的。
安装:
1、先去官网下载最新版的 GraphicsMagick :
http://www.graphicsmagick.org/index.html
如下图所示,把 tar.gz 的安装包下载回自己的电脑后再通过 FTP 上传到 Centos Linux 服务器(目前安装的是 1.3.29 ,也是当前最新的版本):

2、Ricky 个人是喜欢把一些临时文件放到 /tmp 目录下,所以请使用 FTP 把 tar.gz 的安装包上传到 /tmp 目录,然后解压:
[root@host ~]# cd /tmp [root@host tmp]# tar -zxvf GraphicsMagick-1.3.29.tar.gz
3、在安装 GraphicsMagick 前还要安装或更新一些依赖包:
[root@host tmp]# yum install libpng libpng-devel zlib freetype -y [root@host tmp]# yum update libpng libpng-devel zlib freetype -y
4、同时还要安装一个名为 jpegsrc 的小软件。
( 1 )如下图所示,复制 jpegsrc 的下载链接(目前安装的是 v9c ,也是当前最新的版本):

( 2 )先使用 yum 安装 wget(如果已经安装过,可忽略这步):
[root@host tmp]# yum -y install wget
( 3 )Ricky 个人是喜欢把一些临时文件放到 /tmp 目录下,所以进入 /tmp 目录,使用 wget 命令把 jpegsrc 的 tar.gz 包下载到这里,然后解压(或者下载到 PC 后,再通过 FTP 上传至 CentOS Linux ):
[root@host tmp]# cd /tmp [root@host tmp]# wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz [root@host tmp]# tar -zxvf jpegsrc.v9c.tar.gz
( 4 )移除掉 libjpeg 和 libjpeg-devel 这两个 rpm 包(当前 yum 安装的这两个包版本太低,新版 GM 不支持,所以才需要额外安装 jpegsrc ):
[root@host tmp]# yum remove -y libjpeg libjpeg-devel
( 5 )进入解压出来的文件夹,在这里执行编译安装操作:
[root@host tmp]# cd jpeg-9c/ [root@host jpeg-9c]# ./configure [root@host jpeg-9c]# make && make install
5、进入 /usr/lib64 目录,如果存在 libjpeg.so 和 libjpeg.so.62 这两个软链接,请删除掉:
[root@host jpeg-9c]# cd /usr/lib64 [root@localhost lib64]# ls -lh | grep libjpeg.so lrwxrwxrwx. 1 root root 17 Nov 21 08:49 libjpeg.so -> libjpeg.so.62.1.0 lrwxrwxrwx. 1 root root 17 Aug 14 16:10 libjpeg.so.62 -> libjpeg.so.62.1.0 -rwxr-xr-x. 1 root root 279K Jun 10 2014 libjpeg.so.62.1.0 [root@host lib64]# rm -f libjpeg.so libjpeg.so.62
否则 GraphicsMagick 在使用过程中会报如下错误:
gm identify: Wrong JPEG library version: library is 62, caller expects 90
6、重新在 /usr/lib64 目录下建立有关于 libjpeg.so 的软链接:
[root@host lib64]# ln -s /usr/local/lib/libjpeg.so.9.3.0 /usr/lib64/libjpeg.so [root@host lib64]# ln -s /usr/local/lib/libjpeg.so.9.3.0 /usr/lib64/libjpeg.so.9 [root@host lib64]# ln -s /usr/local/lib/libjpeg.so.9.3.0 /usr/lib64/libjpeg.so.9.3.0
7、编译安装 GraphicsMagick :
[root@host lib64]# cd /tmp/GraphicsMagick-1.3.29/ [root@host GraphicsMagick-1.3.29]# ./configure --prefix=/usr/local/GraphicsMagick --enable-shared=no --enable-static=yes [root@host GraphicsMagick-1.3.29]# make && make install ./configure 命令(更多安装细节请参见 GraphicsMagick 官网):
8、删除掉这两个文件夹:
[root@host GraphicsMagick-1.3.29]# cd .. [root@host tmp]# rm -rf GraphicsMagick-1.3.29/ jpeg-9c/
9、添加 OpenMP 线程设置,并执行 source 命令使其马上生效:
[root@host tmp]# echo "export OMP_NUM_THREADS=4" >> /etc/profile [root@host tmp]# source /etc/profile
至此,安装完成。
使用:
1、查看 GraphicsMagick 所支持的图片格式:
[root@host tmp]# cd /usr/local/GraphicsMagick/bin/ [root@host bin]# ./gm convert -list formats Format L Mode Description -------------------------------------------------------------------------------- 3FR S r-- Hasselblad Photo RAW 8BIM P rw- Photoshop resource format 8BIMTEXT P rw- Photoshop resource text format 8BIMWTEXT P rw- Photoshop resource wide text format APP1 P rw- Raw application information APP1JPEG P rw- Raw JPEG binary data ART S rw- PFS: 1st Publisher ARW S r-- Sony Alpha DSLR RAW AVS U rw+ AVS X image B S rw+ Raw blue samples BMP P rw- Microsoft Windows bitmap image BMP2 P -w- Microsoft Windows bitmap image v2 BMP3 P -w- Microsoft Windows bitmap image v3 C S rw+ Raw cyan samples CACHE U --- Magick Persistent Cache image format CALS S rw- Continuous Acquisition and Life-cycle Support Type 1 image Specified in MIL-R-28002 and MIL-PRF-28002 CAPTION P r-- Image caption CIN S rw- Cineon Image File CMYK S rw+ Raw cyan, magenta, yellow, and black samples CMYKA S rw+ Raw cyan, magenta, yellow, black, and opacity samples CR2 S r-- Canon Photo RAW CRW S r-- Canon Photo RAW CUR S r-- Microsoft Cursor Icon CUT S r-- DR Halo DCM S r-- Digital Imaging and Communications in Medicine image See http://medical.nema.org/ for information on DICOM. DCR S r-- Kodak Photo RAW DCX S rw+ ZSoft IBM PC multi-page Paintbrush DNG S r-- Adobe Digital Negative DPX P rw- SMPTE 268M-2003 (DPX 2.0) See http://www.smtpe.org/ for information on DPX. EPDF P rw- Encapsulated Portable Document Format EPI P rw- Adobe Encapsulated PostScript Interchange format EPS P rw- Adobe Encapsulated PostScript EPS2 P -w- Adobe Level II Encapsulated PostScript EPS3 P -w+ Adobe Level III Encapsulated PostScript EPSF P rw- Adobe Encapsulated PostScript EPSI P rw- Adobe Encapsulated PostScript Interchange format ERF S r-- Epson RAW Format EXIF P rw- Exif digital camera binary data FAX P rw+ Group 3 FAX (Not TIFF Group3 FAX!) FITS S rw- Flexible Image Transport System FRACTAL S r-- Plasma fractal image G S rw+ Raw green samples GIF P rw+ CompuServe graphics interchange format (version 89a) GIF87 P rw- CompuServe graphics interchange format (version 87a) GRADIENT P r-- Gradual passing from one shade to another GRAY S rw+ Raw gray samples GRAYA S rw+ Raw gray samples + alpha HISTOGRAM P -w- Histogram of the image HRZ S r-- HRZ: Slow scan TV HTML S -w- Hypertext Markup Language and a client-side image map ICB S rw+ Truevision Targa image ICC P rw- ICC Color Profile ICM P rw- ICC Color Profile ICO S r-- Microsoft Icon ICON S r-- Microsoft Icon IDENTITY P r-- Hald CLUT identity image IMAGE P r-- GraphicsMagick Embedded Image INFO S -w+ Image descriptive information and statistics IPTC P rw- IPTC Newsphoto IPTCTEXT P rw- IPTC Newsphoto text format IPTCWTEXT P rw- IPTC Newsphoto text format JNG S rw- JPEG Network Graphics (libpng 1.5.13, zlib 1.2.7) See http://www.libpng.org/pub/mng/ for information on JNG. JNX S r-- JNX: Garmin tile storage format JPEG P rw- Joint Photographic Experts Group JFIF format (IJG JPEG 90) JPG P rw- Joint Photographic Experts Group JFIF format (IJG JPEG 90) K S rw+ Raw black samples K25 S r-- Kodak Photo RAW KDC S r-- Kodak Photo RAW LABEL P r-- Image label M S rw+ Raw magenta samples M2V S -w+ MPEG Video Stream MAC S r-- Mac Paint MAP U rw- Colormap intensities and indices MAT S rw+ MATLAB Level 4.0-7.0 image formats MATTE S -w+ MATTE raw opacity format MEF S r-- Mamiya Photo RAW MIFF P rw+ Magick Image File Format MNG S rw+ Multiple-image Network Graphics (libpng 1.5.13, zlib 1.2.7) See http://www.libpng.org/pub/mng/ for information on MNG. MONO S rw- Bi-level bitmap in least-significant-byte first order MPC U rw+ Magick Persistent Cache image format MPEG S -w+ MPEG Video Stream MPG S -w+ MPEG Video Stream MRW S r-- Minolta Photo RAW MTV U rw+ MTV Raytracing image format MVG S rw- Magick Vector Graphics NEF S r-- Nikon Electronic Format NULL P rw- Constant image of uniform color O S rw+ Raw opacity samples ORF S r-- Olympus Photo RAW OTB S rw- On-the-air bitmap P7 S rw+ Xv thumbnail format PAL S rw- 16bit/pixel interleaved YUV PALM U r-- Palm pixmap PAM P rw+ Portable Arbitrary Map format PBM P rw+ Portable bitmap format (black/white) PCD S rw- Photo CD PCDS S rw- Photo CD PCL S -w+ Page Control Language PCT S rw- Apple Macintosh QuickDraw/PICT PCX S rw- ZSoft IBM PC Paintbrush PDB S rw+ Palm Database ImageViewer Format PDF P rw+ Portable Document Format PEF S r-- Pentax Electronic File PFA P --- Postscript Type 1 font (ASCII) PFB P --- Postscript Type 1 font (binary) PGM P rw+ Portable graymap format (gray scale) PICON S rw- Personal Icon PICT S rw- Apple Macintosh QuickDraw/PICT PIX S r-- Alias/Wavefront RLE image format PLASMA S r-- Plasma fractal image PNG P rw- Portable Network Graphics (libpng 1.5.13, zlib 1.2.7) See http://www.libpng.org/ for information on PNG.. PNG00 P rw- PNG that inherits type and depth from original (libpng 1.5.13, zlib 1.2.7) PNG24 P rw- 24-bit RGB PNG, opaque only (libpng 1.5.13, zlib 1.2.7) PNG32 P rw- 32-bit RGBA PNG, semitransparency OK (libpng 1.5.13, zlib 1.2.7) PNG48 P rw- opaque or binary transparent 48-bit RGB (libpng 1.5.13, zlib 1.2.7) PNG64 P rw- opaque or transparent 64-bit RGBA (libpng 1.5.13, zlib 1.2.7) PNG8 P rw- 8-bit indexed PNG, binary transparency only (libpng 1.5.13, zlib 1.2.7) PNM P rw+ Portable anymap PPM P rw+ Portable pixmap format (color) PREVIEW S -w- Show a preview an image enhancement, effect, or f/x PS P rw+ Adobe PostScript PS2 P -w+ Adobe Level II PostScript PS3 P -w+ Adobe Level III PostScript PWP U r-- Seattle Film Works R S rw+ Raw red samples RAF S r-- Fuji Photo RAW RAS S rw+ SUN Rasterfile RGB S rw+ Raw red, green, and blue samples RGBA S rw+ Raw red, green, blue, and matte samples RLA U r-- Alias/Wavefront image RLE U r-- Utah Run length encoded image SCT U r-- Scitex HandShake SFW U r-- Seattle Film Works SGI S rw- Irix RGB image SHTML S -w- Hypertext Markup Language and a client-side image map SR2 S r-- Sony Photo RAW SRF S r-- Sony Photo RAW STEGANO S r-- Steganographic image SUN S rw+ SUN Rasterfile SVG S --- Scalable Vector Graphics SVGZ S --- Scalable Vector Graphics (ZIP compressed) TEXT S rw+ ASCII Text TGA S rw+ Truevision Targa image TILE P r-- Tile image with a texture Use the syntax "-size WIDTHxHEIGHT TILE:imagename" to tile the specified tile image over a canvas image of size WIDTHxHEIGHT. TIM S r-- PSX TIM TOPOL S r-- TOPOL X Image TTF P --- TrueType font TXT S rw+ ASCII Text UIL U -w- X-Motif UIL table UYVY S rw- 16bit/pixel interleaved YUV VDA S rw+ Truevision Targa image VICAR S rw- VICAR rasterfile format VID S rw+ Visual Image Directory VIFF S rw+ Khoros Visualization image VST S rw+ Truevision Targa image WBMP S rw- Wireless Bitmap (level 0) image WPG S r-- Word Perfect Graphics X3F S r-- Foveon X3 (Sigma/Polaroid) RAW XBM S rw- X Windows system bitmap (black/white) XC P r-- Constant image uniform color XCF S r-- GIMP image XMP P rw- Adobe XML metadata XPM S rw- X Windows system pixmap (color) XV S rw+ Khoros Visualization image Y S rw+ Raw yellow samples YUV S rw- CCIR 601 4:1:1 or 4:2:2 (8-bit only) Meaning of 'L': P=Primary, S=Stable, U=Unstable [root@host bin]#
2、使用 GraphicsMagick 读取(识别)一个 jpg 文件:
[root@host bin]# ./gm identify /tmp/1.jpg /tmp/1.jpg JPEG 98x59+0+0 DirectClass 8-bit 1.9Ki 0.000u 0m:0.000004s
3、使用 GraphicsMagick 读取(识别)一个 png 文件:
[root@host bin]# ./gm identify /tmp/1.png /tmp/1.png PNG 300x240+0+0 DirectClass 8-bit 29.6Ki 0.000u 0m:0.000004s
发表评论?