matplotlib库
示例http://matplotlib.org/gallery.html
文档http://matplotlib.org/contents.html
中文入门http://azaleasays.com/2010/04/27/matplotlib-beginner-guide/
http://wenku.baidu.com/view/1f575c649b6648d7c1c74682.html
安装方法:yum install python-matplotlib
或者
easy_install matplotlib
或者从源码安装
python setup.py build
python setup.py install
后两种方式都可能会遇到一些缺少freetype包、libpng包的错误,用yum安装这些依赖包即可
适用性:二维、三维的数据报表
Python Imaging Library
官网http://www.pythonware.com/products/pil/
手册http://www.pythonware.com/library/pil/handbook/introduction.htm
Image class
[读|写]
打开图片 im = Image.open('/tmp/Image.png')
从打开的文件读 im = Image.open(open('/tmp/Image.png','rb'))
从字符串读 im = Image.open(StringIO.StringIO(buffer))
从tar归档读 im = Image.open(TarIO.TarIO('Imaging.tar','Imaging/test/lena.ppm'))
图片信息 im.format, im.size, im.mode
显示图片 im.show()
保存图片 im.save('/tmp/newImage.png')
缩略图 im.thumbnail((length,width))
im.save('fileName', 'JPEG')
[剪|粘|拆|并]
剪切 region = im.crop((startX,startY,endX,endY))
旋转 region = region.transpose(Image.ROTATE_180)
粘贴 im.paste(region,(leftX,upperY,rightX,lowerY))
分解 rtList = im.split()
合并 im = Image.merge('RGB', (rtList[0],rtList[1],rtList[2]))
[几何转换]
改变大小 newIm = im.resize((128,128))
逆时针旋转45° newIm = im.rotate(45)
水平中轴翻转 newIm = im.transpose(Image.FLIP_LEFT_RIGHT)
竖直中轴翻转 newIm = im.transpose(Image.FLIP_TOP_BOTTOM)
逆时针旋转90° newIm = im.transpose(Image.ROTATE_90)
逆时针旋转180° newIm = im.transpose(Image.ROTATE_180)
逆时针旋转270° newIm = im.transpose(Image.ROTATE_270)
[颜色转换]
变色 newIm = im.convert('L') #('L'/'RGB'/other,other之间需要用'RGB'中转)
[图片强化]
filter强化 newIm = im.filter(ImageFilter.DETAIL) #ImageFilter.DETAIL效果没看出来,ImageFilter中其他预定义的强化过滤器没试过
像素强化 newIm = im.point(lambda i: i * 1.2) #可以和paste组队
图片强化 enh = ImageEnhance.Contrast(im) #用于im对比度Contrast的对象(ImageEnhance中还有亮度brightness、锐利sharpness的对象)
newIm = enh.enhance(3)
[图片队列]
处理动态图 gifIm = Image.open('debug.gif')
im1 = gifIm
gifIm.seek(1) #跳到第二帧(通过异常EOFError捕捉来确定是否到最后一帧)
im2 = gifIm
ImageDraw模块:绘图(坐标原点:图片左上角 x轴正方向:水平向右 y轴正方向:竖直向下)
弧线 drawObj.arc(xy,start,end,options)
位图 drawObj.bitmap(xy,bitmap,optiosn)
弦 drawObj.chord(xy,start,end,options)(椭)圆 drawObj.ellipse(xy,options)(这个函数fill选项设定的是内部颜色,outline是边界颜色,第一个参数(startX,startY,endX,endY)中的两个点是矩形的左上角和右下角,椭圆在矩形中形成)
直线 drawObj.line(xy,options)
没试过 drawObj.pieslice(xy,start,end,options)
点 drawObj.point(xy,options)
多边形 drawObj.polygon(xy,options)
矩形 drawObj.rectangle(box,options)
文本 drawObj.text(position,string,options) (position是string的左上角,可通过drawObj.textsize(string,options)返回的文本大小调整positoin)
#encoding=utf-8
import Image, ImageDraw, sys, pprint
if __name__ == '__main__':
im = Image.new('L', (400,400), 255)
draw = ImageDraw.Draw(im)
draw.line((0,0,400,400),fill=0)
draw.line((0,400,400,0),fill=0)
draw.ellipse((0,0,25,25), outline=0)
draw.ellipse((375,375,400,400), outline = 0)
draw.ellipse((200,200,300,300), outline = 0)
draw.ellipse((0,350,25,400), outline = 128)
textStr = 'hello,pil!'
textSize = draw.textsize(textStr)
draw.text((200-textSize[0]/2.0,200-textSize[1]/2.0),textStr,fill=128)
im.save('/tmp/PIL.jpg')
Mode – gray scale “L”, color “RGBA” “RGB”, etc.
1 (1-bit pixels, black and white, stored with one pixel per byte)
L (8-bit pixels, black and white)
P (8-bit pixels, mapped to any other mode using a colour palette)
RGB (3x8-bit pixels, true colour)
RGBA (4x8-bit pixels, true colour with transparency mask)
CMYK (4x8-bit pixels, colour separation)
YCbCr (3x8-bit pixels, colour video format)
I (32-bit integer pixels)
F (32-bit floating point pixels)
Color – specified as 32bit value, tuple, or string
myColor = (255, 0, 0, 128) # full red, with 50% alpha
myColor = 0x7f0000ff # full red, with 50% alpha
myColor = “#00ff00” # web color
myColor = “rgb (75%, 0%, 0%)”
myColor = “hsl (0, 100%, 50%)”
RGB颜色查询http://www.wescn.com/tool/color_3.html
下面两个几何函数写得好艰难呀,数学知识差点全部还给老师了...
def LineCrossCircleV1(centerPos, r, k):
'''
求【以centerPos为圆心、r为半径的圆】和【以k为斜率穿过centerPos的直线】的交叉点
'''
tmpV = math.sqrt(r**2/(1+k**2))
rtX1 = centerPos[0] + tmpV
rtY1 = k*(rtX1-centerPos[0]) + centerPos[1]
rtX2 = centerPos[0] - tmpV
rtY2 = k*(rtX2-centerPos[0]) + centerPos[1]
return (rtX1,rtY1),(rtX2,rtY2)
def LineCrossCircleV2(centerPos, r, linePos, k):
'''
求【以centerPos为圆心、r为半径的圆】和【以k为斜率穿过linePos的直线】的交叉点
'''
k = float(k)
r = float(r)
x0, y0 = float(centerPos[0]), float(centerPos[1])
x1, y1 = float(linePos[0]), float(linePos[1])
#a*x**2+b*x+c=0
a = k**2+1
b = 2*k*(y1-k*x1-y0)-2*x0
c = (y1-k*x1-y0)**2+x0**2-r**2
#(x+b/(2a))**2 = (b/(2*a))**2-c/a
squareRt = (b/(2*a))**2-c/a
sqrtRt = math.sqrt(squareRt)
rtX1 = -b/(2*a) + sqrtRt
rtY1 = k*(rtX1-x1) + y1
rtX2 = -b/(2*a) - sqrtRt
rtY2 = k*(rtX2-x1) + y1
#print 'centerPos:%s r:%s linePos:%s k:%s --> (%s,%s),(%s,%s)' % (centerPos,r,linePos,k,rtX1,rtY1,rtX2,rtY2)
return (rtX1,rtY1),(rtX2,rtY2)
(花了三天时间熟悉PIL并用PIL写了个绘制拓扑图的程序,需要的可以留email,哈哈--2012.9.22 0:10)
PS:
1.raise ImportError("The _imagingft C module is not installed")
解决办法:从官网下载源码重新编译安装...
2.中文字体文件 http://code.google.com/p/chinesepuppylinux/downloads/detail?name=wqy-microhei.ttc