图片处理 python

1. 图片分辨率表示  Width * Height

2. cv2读入图片及缩放方式

    读入图片存储类型: numpy array -->  Height * Width * Channel

    import cv2 as cv

    # 读入原图片
    img = cv.imread('./0000001-imgL.png')
    # 打印出图片尺寸
    print(img.shape)
    # 将图片高和宽分别赋值给x,y
    x, y = img.shape[0:2]

    # 显示原图
    cv.imshow('OriginalPicture', img)

    # 缩放图片,输出尺寸格式为(宽,高),    最近邻插值法缩放
    img_test1 = cv.resize(img, (512,384),interpolation=cv.INTER_NEAREST)
    cv.imshow('resize0', img_test1)
    cv.waitKey()

3. PIL读入图片及缩放方式

    读入图片存储类型: ImageFile --> Width * Height

    from PIL import Image

    img = Image.open('./0000001-imgL.png')

    #输出尺寸格式为(宽,高)
    img = img.resize((512, 384), Image.ANTIALIAS)

4. 转换

首先注意,不同读取方式颜色通道不同

PIL读取后,颜色通道为RGB

cv2读取后,颜色通道为BGR

1)PIL --> cv2

    from PIL import Image
    import cv2 as cv
    img = Image.open('./0000001-imgL.png')
    # 转变为数组,RGB
    img = np.array(img)
    # 交换通道,BGR
    img1 = img.copy()  # 改变img1的时候不改变img
    img1[:, :, 0] = img[:, :, 2]
    img1[:, :, 1] = img[:, :, 1]
    img1[:, :, 2] = img[:, :, 0]

    cv.imshow('before', img)
    cv.imshow('after', img1)
    cv.waitKey()

呈现结果对比

扫描二维码关注公众号,回复: 9843337 查看本文章

2)cv2 -->PIL

    img = cv.imread('./0000001-imgL.png')
    img = Image.fromarray(img.astype('uint8')).convert('RGB')
    import matplotlib.pyplot as plt
    plt.imshow(img)
    plt.show()
发布了31 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weijie_home/article/details/104546817