zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    Linux:删除程序
    Linux:目录操作
    Linux:加载硬盘
    mysql:查询结果添加序列号
    mysql:结果集去重
    mysql:字符串转换为日期类型
    MVC:上传文件时限制文件类型
    WebApi:WebApi的Self Host模式
    WebApi:过滤器的种类
    几种知名开源富文本编辑器记录和对比(仅供参考)
  • 原文地址:https://www.cnblogs.com/xiaohai2003ly/p/8778618.html
Copyright © 2011-2022 走看看