python Image中对图像的一些操作

来源:互联网 发布:网络剧 余罪 林宇婧 编辑:程序博客网 时间:2024/05/21 14:50

Python PIL库中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)

此表来自http://blog.csdn.net/frank_good/article/details/52248750

对其中的方法都试了试:

from PIL import Image

from PIL import ImageFilter

#导入图片库

im1 = Image.open("img00006.jpg")

im2 = Image.open("img00310.jpg")  #打开图片

print("image format: "+im2.format)  #输出图片格式  JPEG  png

print("image size: ",im2.size)  #输出图片大小  image size:  (960, 540)

print("image mode: "+im2.mode)  #  image mode: RGB

im2 = im1.point(lambda p: p * 1.4)  #调整图片亮度信息  对每个点进行40%的加强  大于1会变亮;小于1会变暗

#裁剪

box=(100,100,500,500)  #设置要裁剪的区域 

region=im1.crop(box)  #此时,region是一个新的图像对象。

region.show()

#图片裁剪

r,g,b=im1.split()#分割成三个通道  ,此时r,g,b分别为三个图像对象。

im=Image.merge("RGB",(b,g,r))#  通道合并 (rgb)是正常的合并顺序,(b,g,r)将b,r两个通道进行翻转。颜色将会发生变化

im1.paste(region,box)  #粘贴box大小的region到原先的图片对象中。

im3=Image.blend(im1,im2,0.5)  #两张图片合成

im4=im3.rotate(45) #逆时针旋转45

out = im2.transpose(Image.FLIP_LEFT_RIGHT)#左右对换。

out= im2.transpose(Image.FLIP_TOP_BOTTOM)#上下对换

out=im2.resize((128,128))   #  resize128*128像素大小

im2=im2.convert("RGBA") #image mode转换

im2.save("image2.jpg")  #图片保存

print(im2.getpixel((900,500))) #输出图片中某一点的像素值(133, 151, 175, 255)

im2.putpixel((900,500),(255,0,0))  #更改图片中某一点的像素值 (位置,新的像素值)

print(im2.getpixel((900,500))) #(255, 0, 0, 255)

imgfilted=im2.filter(ImageFilter.EMBOSS)#给图片加滤镜  需要引入ImageFilter模块

imgfilted.show()

 

 

阅读全文
0 0