Python第三方库使用 —— PIL

                       

PIL:Python Image Library

1. Image.fromarray ⇔   ⇔           ⇔ ⇔ ⇔      \Leftrightarrow        np.asarray

def read_image(path): img = Image.open(path) img = img.convert('L') return np.asarray(img, dtype='float64')/255.def save_image(array, path): array[array > 255] = 255 array[array < 0] = 0 array.convert('RGB').save(path)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2. 重要模块及其成员函数

  • Image.fromarray()

    顾名思义,将二维数组转换为图像。

    from PIL import Imageimport numpy as nparr = (np.eye(200)*255).astype('uint8')im = Image.fromarray(arr)imrgb = Image.merge('RGB', (im, im, im))imrgb.show()
        
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
     
      ![这里写图片描述](https://img-blog.csdn.net/20151210104248494) 
  • PIL 读取获得的图像矩阵与 numpy 下的多维数组

    import numpy as npfrom PIL import Imageimg = Image.open(open('./images/3wolfmoon.jpg'))   # Image.open 接受一个文件句柄img = np.asarray(img, dtype='float64')/255.img.shape   # (639, 516, 3)   # 做到这一步还不够,如果是彩色图像   # img.shape = (height, width, ndim)   # 并不是 numpy 中所习惯的维度配置img = img.transpose(2, 0, 1)img.shape   # (3, 639, 516)
        
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值