我正在尝试使用色差将一个图像的所有像素更改为特定颜色,以保持不同的色调和轮廓。
我已经尝试遍历所有像素,从要替换的像素中减去颜色元组,然后替换它,但结果得到的是灰/灰图像。
我要使用这种方法来查找颜色差异
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
from trying_out_colors import get_main_color
def find_color_difference_with_tuple(tuple_1, tuple_2):
a, b, c = tuple_1
d, e, f = tuple_2
rgb_1 = sRGBColor(a, b, c)
lab_1 = convert_color(rgb_1, LabColor)
rgb_2 = sRGBColor(d, e, f)
lab_2 = convert_color(rgb_2, LabColor)
difference = delta_e_cie2000(lab_1, lab_2)
return difference
然后替换颜色,这是我使用的方法
import numpy
from PIL import Image
from trying_out_colors import get_main_color
from color_difference import find_color_difference_with_tuple
source_image = "source.png"
destination_image = "destination.png"
# get_main_color function finds the dominant color of an image
value = get_main_color(source_image)
destination = Image.open(destination_image)
new = Image.new("RGB", destination.size, 0xffffff)
width, height = destination.size
for x in range(width):
for y in range(height):
destiny = destination.getpixel((x, y))
color_diff = find_color_difference_with_tuple(value, destiny)
total_diff = tuple(numpy.subtract(value, color_diff))
total_diff = tuple([int(x) for x in total_diff])
new.putpixel((x, y), total_diff)
new.save("new_image.png")
这是源图像,我正在尝试从源图像中获取主色,然后使用主色与目标图像的每个像素之间的色差,替换所有像素以使目标图像与源图像具有相同的肤色。 我希望我能很好地解释我的问题。
此外,如果有任何方法可以使代码运行更快,我将非常感谢。这大约需要5分钟才能运行。