PIL_图像通道的分离合并操作

def Binarization():

    img = Image.open("1.png")
    r, g, b = img.split()  # 分离三通道
    # pic = Image.merge('RGB', (r, g, b))  # 合并三通道

    limg = img.convert('L') # 转换成单通道的灰度图

    threshold = 4
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)

    bimg = limg.point(table, "1") # 二值化,自定义阀值写入table

    bimg_L = bimg.convert('L') # 将二值化后的图像转化成灰度图才能拼接

    bimg_3 = Image.merge('RGB', (bimg_L, bimg_L, bimg_L)) # 将二值化图像拼接成三通道

    plt.imshow(bimg_3)
    plt.show()

    bimg.save("2.jpg")

    im = np.array(img)
    print(im.shape)

猜你喜欢

转载自blog.csdn.net/Gentlemanman/article/details/86519941
今日推荐