matplotlib庫
官網http://matplotlib.org/
樣本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-8import Image, ImageDraw, sys, pprintif __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] + tmpVrtY1 = 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=0a = k**2+1b = 2*k*(y1-k*x1-y0)-2*x0c = (y1-k*x1-y0)**2+x0**2-r**2#(x+b/(2a))**2 = (b/(2*a))**2-c/asquareRt = (b/(2*a))**2-c/asqrtRt = math.sqrt(squareRt)rtX1 = -b/(2*a) + sqrtRtrtY1 = k*(rtX1-x1) + y1rtX2 = -b/(2*a) - sqrtRtrtY2 = 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