圖片處理GraphicsMagick & ImageMagick

用Java的ImageIO處理一個大於15M的JPG文件,內存溢出。php

因而尋求新的解決方案,用ImageMagicK,發現處理圖片速度不使人滿意,因而切換至GraphicsMagicK,以下是總結。java

GraphicsMagick is a fork of ImageMagick. GraphicsMagick has a number of advantages compared to ImageMagick, the most prominent is it's superior performance.」  -from im4java development guide.app

以下是快速的總結:ide

GraphicsMagick(gm)是ImageMagick(im)派生出來的,性能好。性能

gm和im都須要安裝依賴的庫,才能處理如jpeg,png圖片。測試

依賴的庫有:zlib-1.2.8.tar.gz;libpng-1.6.23.tar.gz;jpegsrc.v9b.tar.gz, 須要首先安裝。ui

ImageMagick安裝直接能夠用yum安裝,如 list|grep -i imagemagickthis

GraphicsMagick安裝須要本地編譯安裝,首先執行./configure 看看最後有沒有png, jpeg, zlib.(重要)spa

GraphicsMagick與ImageMagick的安裝相互獨立,但依賴的包相同。code

性能上,15Mjpg文件resize,gm快~20%(4U, 4G), 而在一臺性能較差的虛擬機gm要比im快6倍(2U,1G),

圖片越大,resize的尺寸越大,性能差異越明顯。機器配置相差越大,性能差異越大。

性能測試對比

在一臺2U1G VirtualBox Linux下resize一個15M的jpg圖片。分別用gm和im進行操做,而後對比。

[root@testserver image]# uname -a
Linux testserver 2.6.32-431.el6.i686 #1 SMP Fri Nov 22 00:26:36 UTC 2013 i686 i686 i386 GNU/Linux

[root@testserver image]# free -m
             total       used       free     shared    buffers     cached
Mem:          1178        458        719          0         17        302
-/+ buffers/cache:        138       1039
Swap:         2015        137       1878
[root@testserver image]# gm identify test-bigjpg.jpg
test-bigjpg.jpg JPEG 9864x13703+0+0 DirectClass 8-bit 14.8Mi 0.000u 0:01

 

resize 操做對比:
[root@testserver image]# ./test.sh test-bigjpg.jpg 2048
elapsed 25s using gm
elapsed 154s using im

Identify -verbose 操做對比:

[root@testserver image]# identify -verbose 1464755573477_45.jpg
Image: 1464755573477_45.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 13925x6459+0+0
  Resolution: 200x200
  Print size: 69.625x32.295
  Units: PixelsPerInch
  Type: ColorSeparation
  Endianess: Undefined
  Colorspace: CMYK
  Depth: 8-bit
  .....//此處省略
  User time: 5.940u
  Elapsed time: 0:34.689
  Version: ImageMagick 6.7.2-7 2016-05-09 Q16 http://www.imagemagick.org
[root@testserver image]# gm identify -verbose 1464755573477_45.jpg
Image: 1464755573477_45.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Geometry: 13925x6459
  Class: DirectClass
  Type: color separated
  Depth: 8 bits-per-pixel component
  ... //此處省略
  User Time: 6.580u
  Elapsed Time: 0:08
  Pixels Per Second: 11.8Mi

附上測試腳本的代碼:

#!/bin/sh
starttime=`date '+%s'`
gm convert $1 -resize $2 $1.resize$2.jpg
stoptime=`date '+%s'`
echo elapsed `expr $stoptime - $starttime`s using gm

starttime=`date '+%s'`
convert $1 -resize $2 $1.resize$2.jpg
stoptime=`date '+%s'`
echo elapsed `expr $stoptime - $starttime`s using im

 

Im4java

Im4java同時支持GM 和 IM

With im4java, you have three options if you want to use GraphicsMagick:

  • use GraphicsMagick explicitely, passing the command at object-creation: GraphicsMagickCmd cmd = new GraphicsMagickCmd("convert");.
  • use GraphicsMagick explicitely, using wrapper classes: ConvertCmd cmd = new ConvertCmd(true);.
  • decide at runtime: setting the system-property im4java.useGM to true will select GraphicsMagick at runtime. You can use this feature to compare the results and timings of both toolsets, provided that the commandline is compatible.

Reference:

GraphicsMagick: http://www.graphicsmagick.org/

ImageMagick:http://www.imagemagick.org/script/index.php

Delegatge lib: ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/delegates/

相關文章
相關標籤/搜索