python, Image


PIL: Python Image Library, python平台的圖像處理庫,要使用Image首先要從PIL庫導入Image:

from PIL import Image

如果沒有安裝PIL的包,導入會出錯。所以要先安裝包

PIL:Python Imaging Library(僅支持到python2.7)

Pillow:支持最新的Python 3.*

Python3.*的版本直接安裝Pillow:pip install Pillow

 

Image是PIL下的一個類,具體的使用如下:

from PIL import Image

# 讀取圖片

im = Image.open(r''D:\kolor.jpg'')

#查看圖片信息

im.format, im.size, im.mode

# 顯示圖片

im.show()

# 保存圖片, 參數:保存的地址和名稱,圖片格式

im.save(r'D:\kolor.jpg', 'JPEG')

#創建新圖片

im=Image.new(mode, size) # color的默認值是黑色

im=Image.new(mode, size, color)

如: im=Image.new('RGB', (450, 600), (255,255,255))  # 'RGB'是紅(RED)綠(GREEN)藍(BLUE)模式

#兩張圖片相加

Image.blend(im1, im2, alpha)  # alpha是im1和im2的比例參數

#點操作

im.point(function) # function接收一個參數,並對圖片的每個點執行這個函數

如:im.point(lambad i: i*1.5) #對每個點進行50%的加強

#圖片裁剪

box=(100,100,500,500)  # 設置要裁剪的區域

region=im.crop(box)

#圖片黏貼

im.paste(im1, box) #把im1的box區域黏貼到im中去

#通道分離

r,g,b=im.split() #分割成三個通道,此時r,g,b分別是三個圖像對象

#通道合並

im=Image.merge('RGB', (b,g,r)) #將b, r兩個通道進行反轉

#改變圖片的大小

im.resize((128,128))

#縮小圖片

im.thumbnail(())

im.thumbnail((w//2, h//2))  # 縮小50%

#旋轉圖像

im.rotate(45) #逆時針旋轉45度

im.transpose(Image.ROTATE_180)

im.transpose(Image.FLIP_LEFT_RIGHT) #左右兌換

im.transpose(Image.FLIP_TOP_BOTTOM) #上下對換

#圖像mode轉換

im.convert('RGBA') #圖像的mode轉換成RGBA類型

#寫某個像素位置的值

im.putpixel((4,4), (255,0,0))

#加濾鏡

im.filter(ImageFilter.BLUE)

 

以下是Image對象的全部方法:

 

save(f,format=None) 保存 如果f是一個file對象,必須指定format(format codes)
convert(mode) 轉換mode  
copy()    
crop(bbox) 剪切 原圖中bbox區域
filter(name) 濾鏡 the name of predefined image enhancement filters
濾鏡名字需要import ImageFilter
getbands() 通道的字符串序列 如RGB圖返回('R', 'G', 'B')
getbbox() 包含非零區域的最小bbox  
getextrema() 最大最小像素點值 min&max pixel value
單通道圖:返回元組(min,max)
多通道圖:返回各個通道的元組組成的元組
getpixel(xy) 取像素點值 坐標xy處的pixel value or a sequence of pixel values
histogram(mask=None)

統計直方圖

單通道圖:返回列表[c0, c1, ...],ci是值為i的像素數

多通道圖:a single sequence that is the concatenation of the sequences for all bands

mask參數:a same-sized mask image of mode "1" or "L"(include only those pixels correspond to nonzero pixels in the mask argument)

offset(dx,dy=None)

平移

Returns a new image the same size as the original, but with all pixels rotated dx in the +x direction,and dy in the +y direction.

If dy is omitted, it defaults to the same value as dx.

paste(i2,where,mask=None) 粘貼圖片 where參數可以是
1 (x,y)坐標對:i2的像素點(0,0)對齊原圖中的(x,y)粘貼,i2超過原圖邊界的部分被拋棄
2 bbox:i2必須和該bounding box大小一致
3 None:i2必須和原圖大小一致
如果i2的mode和原圖不一致,粘貼前會被轉換。
mask參數:a same-sized mask image of mode "1","L" or “RGBA ”(control which pixels get replaced)
paste(color,box=None,mask=None) 填充顏色 如果box省略,整個圖被填充為color色;mask參數同上
point(function) 改變像素點(函數) Returns a new image with each pixel modified.
point(table) 改變像素點(查表) To translate pixels using a table(a sequence of 256n values, where n is the number of bands in the image) lookup
putalpha(band)

改變alpha通道

The pixels of the band image(same-sized,"L" or "1") replace the alpha band(A) of the original image(RGBA) in place.

putpixel(xy, color) 改變單個像素點顏色 Note that this method is relatively slow. For more extensive changes, use paste or theImageDraw module instead.
resize(size,filter=None) 調整大小  
rotate(theta)

旋轉(圍繞圖片中心)

 

Any pixels that are not covered by rotation of the original image are set to black.

show()

顯示圖片

On Unix systems, this method runs the xv image viewer to display the image. 
On Windows boxes,the image is saved in BMP format and can be viewed using Paint. 
This can be useful for debugging.

split()

分離通道

返回各個通道的灰度圖組成的元組
Returns a tuple containing each band of the original image as an image of mode "L". 
For example, applying this method to an "RGB" image produces a tuple of three images, one each for the red, green, and blue bands.

thumbnail(size,filter=None) 縮略圖 Modifies in-place,Preserves aspect ratio
transform(xs, ys, Image.EXTENT, (x0,y0,x1,y1))  

Returns a transformed copy of the image. In the transformed image, the point originally at (x0,y0) will appear at (0,0), and point (x1,y1) will appear at (xs, ys).

transform(xs, ys, Image.AFFINE, (a,b,c,d,e,f)) affine變換

The values a through f are the first two rows of an affine transform matrix.
Each pixel at (x,y) in the resulting image comes from position (ax+by+c,dx+ey+f) in the input
image, rounded to the nearest pixel.

transpose(method) 翻轉旋轉 ROTATE_90/180/270(clockwise), FLIP_TOP_BOTTOM(horizontal), FLIP_RIGHT_LEFT(vertical)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM