matplotlib--python繪製圖表 | PIL--python影象處理
matplotlib庫
安裝方法:yum install python-matplotlib
或者
easy_install matplotlib
或者從原始碼安裝
python setup.py build
python setup.py install
後兩種方式都可能會遇到一些缺少freetype包、libpng包的錯誤,用yum安裝這些依賴包即可
適用性:二維、三維的資料報表
Python Imaging Library
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模組
弧線 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%)”
下面兩個幾何函式寫得好艱難呀,數學知識差點全部還給老師了...
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")
解決辦法:從官網下載原始碼重新編譯安裝...