问题描述
我正在做一个照片编辑项目,我很好奇为什么我的新照片失去了亮度。该程序应该从原始照片中获取 2 张照片。其中一个应该只包含 RED 值,另一个应该包含 BLUE 和 GREEN 值。但是当我把它们放回一起时,亮度与原始图片中的不一样。
这是我的代码:
import io,re,requests
from PIL import Image,ImageOps,ImageEnhance,ImageChops
import cv2
import numpy as np
imgpth ='image.jpg'
#red image
img2 = Image.open(imgpth).convert('RGB')
source = img2.split()
R,G,B = 0,1,2
out = source[G].point(lambda i: i * 0)
source[G].paste(out,None,None)
out = source[B].point(lambda i: i * 0)
source[B].paste(out,None)
img2 = Image.merge(img2.mode,source)
#green and blue image
img = Image.open(imgpth).convert('RGB')
source = img.split()
R,2
out = source[R].point(lambda i: i * 0)
source[R].paste(out,None)
img = Image.merge(img.mode,source)
blend2 = Image.blend(img,img2,0.5)
blend2.show()
输出图像:enter image description here
解决方法
blend2 = Image.blend(img,img2,0.5)
第三个参数 0.5 是每一层的 alpha 级别。本质上,您将每个图层设置为 50% 透明。这有效地降低了亮度。相反,您应该读入 img1
和 img2
,然后将第二个的红色图层设置为第一个的红色图层。
img2[R] = img1[R]