Pillow-读取图片RGB值并还原

2020年08月08日 65点热度 0人点赞 0条评论
from PIL import Image

WIDTH = 0
HEIGHT = 0
IMG_PATH = r'.\1\1.png'

def getImgData():
    global WIDTH
    global HEIGHT
    global IMG_PATH
    img = Image.open(IMG_PATH,'r')
    imginfo = (img.size)
    print(imginfo)
    WIDTH = imginfo[0]
    HEIGHT = imginfo[1]

    lst = list(img.getdata())
    return lst


def run(save_path = ""):
    global WIDTH
    global HEIGHT
    lst = getImgData()
    lst1 = []
    for i in range(0,HEIGHT):
    	lst1.append(lst[WIDTH*i:WIDTH*i+WIDTH])
    newimg = Image.new('RGB',(WIDTH,HEIGHT))

    for val,i in zip(lst1,range(0,len(lst1))):
        for j in range(0,len(val)):
            newimg.putpixel((j,i),(int(val[j][0]),int(val[j][1]),int(val[j][2])))

    newimg.save(save_path + './show.jpg')


run(save_path = "./1")

Hang.Zhang

“舍”是一种觉悟,更是一种自由

文章评论