栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现灰度变换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现灰度变换

def Read_raw(filename,w,h,m):
    #初始化image
    image=[]
    for i in range(m*h):
        image.append([])
        for j in range(w):
            image[i].append(255)
    #读取raw
    with open(filename,'rb') as f:
        data=f.read()
    #写入raw
    for i in range(m*h):
        for j in range(w):
            image[i][j]=data[i*w+j]
    print('读取文件{0}成功'.format(filename))
    return image

def Write_raw(filename,image):
    #将数组image转为bytes
    bt=bytearray()
    for i in range(len(image)):
        for j in range(len(image[i])):
            bt.append(image[i][j])
    #写入raw
    with open(filename,'wb') as f:
        f.write(bt)
    f.close()
    print('写入文件{0}成功'.format(filename))

def change(image):
    red=[]
    green=[]
    blue=[]
    #遍历得像素点的最大值
    L=0
    for i in range(len(image)):
        for j in range(len(image[i])):
            if image[i][j]>L:
                L=image[i][j]
    print('得到像素点最大值为{0}'.format(L))
    print('计算彩色图像素值中---')
    for i in range(len(image)):
        red.append([])
        green.append([])
        blue.append([])
        for j in range(len(image[i])):
            if image[i][j]=L/4 and image[i][j]=L/2 and image[i][j]<3*L/4:
                red[i].append(4*image[i][j]-2*L)
                blue[i].append(0)
                green[i].append(L)
            elif image[i][j]>=3*L/4 and image[i][j]<=L:
                red[i].append(L)
                blue[i].append(0)
                green[i].append(4*L-4*image[i][j])
            else:
                print('error')
    print('计算完成。')
    #非隔行存储
    colour=[]
    for i in range(len(red)):
        colour.append(red[i])
    for i in range(len(green)):
        colour.append(green[i])
    for i in range(len(blue)):
        colour.append(blue[i])
    return colour
image=Read_raw('test2.raw',2823,2320,1)
colour=change(image)
Write_raw('test2_colour.raw',colour)
    
            

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339553.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号