pythonnumpyopencvcolor-wheel

Create color wheel pattern image in python


I am trying to create a color wheel pattern image of given width and height. Something like this:-

enter image description here

How can it be done in a creative pythonic way preferably using opencv and numpy? I found some resources (e.g. here) where inbuilt functions of matloblib are being used.


Solution

  • Utilising the cues in Mark Setchell's answer, I am able to generate a color wheel based image of given width and height.

    Hue:-

    hue = np.fromfunction(lambda i, j: (np.arctan2(i-img_height/2, img_width/2-j) + np.pi)*(180/np.pi)/2,
                                  (img_height, img_width), dtype=np.float)
    

    enter image description here

    Saturation:-

    saturation = np.ones((img_height, img_width)) * 255
    

    enter image description here

    Value:-

    value = np.ones((img_height, img_width)) * 255
    

    enter image description here

    Below is a working code of the same:-

    def make_color_wheel_image(img_width, img_height):
        """
        Creates a color wheel based image of given width and height
        Args:
            img_width (int):
            img_height (int):
    
        Returns:
            opencv image (numpy array): color wheel based image
        """
        hue = np.fromfunction(lambda i, j: (np.arctan2(i-img_height/2, img_width/2-j) + np.pi)*(180/np.pi)/2,
                              (img_height, img_width), dtype=np.float)
        saturation = np.ones((img_height, img_width)) * 255
        value = np.ones((img_height, img_width)) * 255
        hsl = np.dstack((hue, saturation, value))
        color_map = cv2.cvtColor(np.array(hsl, dtype=np.uint8), cv2.COLOR_HSV2BGR)
        return color_map
    

    Resultant Image: enter image description here