找到go语言的源码
https://github.com/nfnt/resize
http://code.google.com/p/gorilla/source/browse/lib/appengine/example/moustachio/resize/resize.go?r=3dbce6e267e9d497dffbce31220a059f02c4e99d
使用’go get ‘安装需要使用git。如果是centos 6 直接安装’yum install git’。但是我的是CentOS 5 还要手动安装一下。
# yum install -y gcc make curl curl-devel zlib-devel openssl-devel perl perl-devel
cpio expat-devel gettext-devel
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
# tar zxvf git-latest.tar.gz
# cd git-2013-03-28/
# autoconf
# ./configure
# make && make install
# git –version
git安装完成,开始测试。我的环境,python2.6,go1.0.2,ImageMagick是C/C++语言开发使用也比较广泛。直接用命令测试。。使用一张150k 510×382的图片做测试。缩成宽300的等比例缩小图。
# go get github.com/nfnt/resize
go 测试代码
package main
import (
"github.com/nfnt/resize"
"image/jpeg"
"os"
)
func main() {
// open "test.jpg"
file, err := os.Open("test.jpg")
if err != nil {
print("Open File Error")
}
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
print("Not image file")
}
file.Close()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(300, 0, img, resize.Lanczos3)
out, err := os.Create("test_go.jpg")
if err != nil {
print("Save Image Error!")
}
defer out.Close()
// write new image to file
jpeg.Encode(out, m, nil)
}
python 测试代码
#!/bin/env python
# -*- coding:utf-8 -*-
# --------------------------------
# Filename: cut_image.py
# Revision: 1.1
# Author: simon-zzm
# Web: www.simonzhang.net
# Email: simon-zzm@163.com
# --------------------------------
import Image
def main():
file = Image.open('test.jpg')
w = file.size[0]
h = file.size[1]
re_data = file.resize((300, int(h/(float(w)/300))),)
re_data.save('test_py.jpg', 'JPEG',)
if __name__ == '__main__':
main()
在linux下使用time进行测试结果
# time python cut_image.py
real 0m0.051s
user 0m0.040s
sys 0m0.009s
# time go run cut_image.go
real 0m2.736s
user 0m2.695s
sys 0m0.039s
# time convert -resize 300x test.jpg test_c.jpg
real 0m0.073s
user 0m0.070s
sys 0m0.002s
-rw-r–r– 1 root root 150332 Jul 16 2012 test.jpg
-rw-r–r– 1 root root 12929 Mar 28 23:13 test_go.jpg
-rw-r–r– 1 root root 13087 Mar 28 23:13 test_py.jpg
-rw-r–r– 1 root root 58591 Mar 28 23:14 test_c.jpg
总结:GO使用这个方法作为图片缩放的处理速度和python处理速度的差距太大了。烦请高手指点如何处理。不过GO的语法还是比较简单,值得学习。之前做的多语言简单累加计算测试,GO效率还是比较高,所以处理业务逻辑处理的效率还应该不错。
源码下载