目录
1.基本逻辑
- 找出灯源通道相对弱势通道的差值,用作通透度
- 找出最暗的通道,用在rgb三通道,形成灰色的基色
2.被处理图片
结构内部有变色的光源,目前为红色偏蓝色,所以根据图片调整参数和计算逻辑如下节
3.处理png的代码
# pip install pillow for PIL
from PIL import Image
def process(pixel):
# 根据红色通道来觉得处理力度
rate = pixel[0] / 255
# 计算需变色区域的无光颜色
m = min(pixel[1], pixel[2])
avg = (pixel[0] + pixel[1] + pixel[2]) // 3
c = round(m*rate + avg*(1-rate))
# 计算需变色区域的变色力度
c_ref = round(pixel[1] * (1-rate))
t = 255 - (pixel[0] - c_ref)
# 只计算需处理的区域
if (pixel[3] == 255):
# 对于普通区域
if (pixel[0] > pixel[1]*1.5):
return (c, c, c, t)
# 对于高亮区域判定规则不同
elif (pixel[0] > 250 \
and pixel[1] < 240 \
and pixel[2] < 240 \
):
return (c, c, c, t)
return (pixel[0], pixel[1], pixel[2], pixel[3])
def change_image_channels(image, image_path, func):
if image.mode == 'RGBA':
width,height = image.size
pixel = image.load()
for y in range(0, height):
for x in range(0, width):
pixel[x,y] = func(pixel[x,y])
image.save(image_path)
return image
if __name__ == "__main__":
image = Image.open("./test.png")
new_image = change_image_channels(image, "./out.png", process)
print(new_image.mode)
4.图片处理后的效果
绿色为在visual studio里面打开png看到的通透效果(有些很弱的红色还残留着,比如左下较亮的侧面,有些区域通透的很夸张比如中间偏下两个两块之间的部分)
在Windows图形编辑器Paint中打开的效果(没看到通透图层,类似白光效果)
5.后期应用
在此图层下叠加变色光源