Python-是否有找到rgb代码互补色的函数或公式? [英] Python- Is there a function or formula to find the complementary colour of a rgb code?

查看:272
本文介绍了Python-是否有找到rgb代码互补色的函数或公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python 3中找到一个好的公式来计算rgb代码的互补色,例如。 a = b的补码。

I have tried to find a good formula in Python 3 to calculate the complementary colour of a rgb code eg. complementary of a = b. Is there any way to do this?

推荐答案

这里是直接计算RGB颜色补数的方法。正如Iva Klass的答案所示,它的结果与使用 colorsys 的算法相同,但在我的测试中,速度提高了约50%。请注意,它适用于任何RGB方案,无论RGB分量是整数还是浮点数(只要每个分量使用相同的范围!)都没关系。

Here's how to calculate the complement of an RGB colour directly. It gives the same results as the algorithm using colorsys as shown in Iva Klass's answer, but in my tests it's about 50% faster. Note that it works for any RGB scheme, it doesn't matter whether the RGB components are integers or floats (as long as each component uses the same range!).

函数 hilo 实现了一个简单的分拣网络

The function hilo implements a simple sorting network to sort the RGB components.

# 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。


Here's a short demo, using 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()



输入图像



input image

这里是 complement_image 的Numpy版本。在我的计算机上,它处理玻璃图像的速度比以前的版本快3.7倍。

Here's a Numpy version of complement_image. On my machine it processes the "Glasses" image about 3.7 times faster than the previous version.

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)

这篇关于Python-是否有找到rgb代码互补色的函数或公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆