我试图在Python 3中找到一个好的公式来计算rgb代码的补色,例如。 a = b 的互补。有什么办法可以做到这一点吗?
以下是如何直接计算 RGB 颜色的补色。它给出的结果与使用
colorsys
的算法相同,如 Iva Klass 的答案所示,但在我的测试中,它的速度大约快 50%。请注意,它适用于任何 RGB 方案,RGB 分量是整数还是浮点数并不重要(只要每个分量使用相同的范围!)。
函数
hilo
实现了一个简单的排序网络来对RGB分量进行排序。
# Sum of the min & max of (a, b, c)
def hilo(a, b, c):
if c < b: b, c = c, b
if b < a: a, b = b, a
if c < b: b, c = c, b
return a + c
def complement(r, g, b):
k = hilo(r, g, b)
return tuple(k - u for u in (r, g, b))
这是一个使用 PIL / Pillow 的简短演示。
#!/usr/bin/env python3
''' Complement the colours in a RGB image
Written by PM 2Ring 2016.10.08
'''
import sys
from PIL import Image
# Sum of the min & max of (a, b, c)
def hilo(a, b, c):
if c < b: b, c = c, b
if b < a: a, b = b, a
if c < b: b, c = c, b
return a + c
def complement(r, g, b):
k = hilo(r, g, b)
return tuple(k - u for u in (r, g, b))
def complement_image(iname, oname):
print('Loading', iname)
img = Image.open(iname)
#img.show()
size = img.size
mode = img.mode
in_data = img.getdata()
print('Complementing...')
out_img = Image.new(mode, size)
out_img.putdata([complement(*rgb) for rgb in in_data])
out_img.show()
out_img.save(oname)
print('Saved to', oname)
def main():
if len(sys.argv) == 3:
complement_image(*sys.argv[1:])
else:
fmt = 'Complement colours.\nUsage: {} input_image output_image'
print(fmt.format(sys.argv[0]))
if __name__ == '__main__':
main()
这是
complement_image
的 Numpy 版本。在我的机器上,它处理“眼镜”图像的速度比以前的版本快约 3.7 倍。
import numpy as np
def complement_image(iname, oname):
print('Loading', iname)
img = Image.open(iname)
#img.show()
in_data = np.asarray(img)
#print(in_data.shape)
print('Complementing...')
lo = np.amin(in_data, axis=2, keepdims=True)
hi = np.amax(in_data, axis=2, keepdims=True)
out_data = (lo + hi) - in_data
out_img = Image.fromarray(out_data)
#out_img.show()
out_img.save(oname)
print('Saved to', oname)
这里有一个简短的演示,使用 scikit-image(和 Numpy)在感知更均匀的 CIELuv 色彩空间中创建互补色。
from skimage.color import rgb2luv, luv2rgb
from skimage.util import img_as_ubyte
luv_data = rgb2luv(in_data) * (1, -1, -1)
out_data = img_as_ubyte(luv2rgb(luv_data))
我认为没有现成的解决方案,但是标准库中有一个 colorsys 模块,它可以提供帮助。
我认为您首先需要将 RGB 转换为 HSV 或 HSL,然后“旋转”色调,如果需要,再转换回 RGB。例如(我不确定是否正确旋转):
from colorsys import rgb_to_hsv, hsv_to_rgb
def complementary(r, g, b):
"""returns RGB components of complementary color"""
hsv = rgb_to_hsv(r, g, b)
return hsv_to_rgb((hsv[0] + 0.5) % 1, hsv[1], hsv[2])
r,g,b = [25,25,25]
def get_complementary(color):
color = color[1:]
color = int(color, 16)
comp_color = 0xFFFFFF ^ color
comp_color = "#%06X" % comp_color
return comp_color
hex_val = "#%02x%02x%02x" % (r,g,b)
h = get_complementary(hex_val)
print("Complementary color",h)
h = h.lstrip('#')
print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
输出:
补色#E6E6E6
RGB = (230, 230, 230)
这很简单而且有效。
color = QColorDialog.getColor()
color_rgb = color.getRgb()
# Complementary color
color_comp = (255-color_rgb[0], # red
255-color_rgb[1], # green
255-color_rgb[2], # blue
color_rgb[3])
RGB: (170, 85, 255, 255)
比较:(85, 170, 0, 255)
可能有一种方法,但这可行。