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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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!).The function
hilo
implements a simple sorting network to sort the RGB components.Here's a short demo, using PIL / Pillow.
input image
output image
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.I don't think there is ready solution for this, but there is a colorsys module in standard library, it can help.
I think you first need to convert RGB into HSV or HSL, then "rotate" hue, and convert back to RGB, if you need. For example (I'm not sure about proper rotating):