Exemplo n.º 1
0
    def iteratePixels(self, matrix):
        arm = ("left_arm", "right_arm")
        print(matrix)
        split = self.width / 2
        hashMap = {}
        counter = 0
        for x in range(self.width / 2, self.width):
            for y in range(100, self.height - 100):
                narray = matrix[x][y]
                red = narray[0]
                green = narray[1]
                blue = narray[2]
                rgb = (red, green, blue)
                if webcolors.rgb_to_name(
                        rgb) in colorOfBlocks and colorOfBlocks not in hashMap:
                    if x < split:
                        tup = (counter, arm[0])
                        hashMap[colorOfBlocks] = tup
                        counter += 1
                    else:
                        tup = (counter, arm[1])
                        hashMap[colorOfBlocks] = tup
                        counter += 1

        return hashMap
def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name
Exemplo n.º 3
0
def get_colour_name(requested_colour):
    new = webcolors.hex_to_rgb(requested_colour)
    try:
        closest_name = webcolors.rgb_to_name(new, spec='html4')
    except ValueError:
        closest_name = closest_colour(requested_colour)
    return closest_name
Exemplo n.º 4
0
def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name
Exemplo n.º 5
0
    def _format_color(self, rgb_value, color_format: str):
        """
        Get the correct color format as specified
        :return:
        """
        if color_format == "hsv":
            # list(np.around(v))
            return str(mcolors.rgb_to_hsv(rgb_value).tolist())

        elif color_format == "hex":
            rgb_value = np.divide(rgb_value, 255)  # give a scale from 0-1
            return mcolors.to_hex(rgb_value)

        elif color_format == "human_readable":
            r0, g0, b0 = int(rgb_value[0]), int(rgb_value[1]), int(
                rgb_value[2])
            try:
                nearest = webcolors.rgb_to_name((r0, g0, b0))
            except ValueError:  # Calculate distances between rgb value and CSS3 rgb colours to determine the closest
                distances = {}
                for k, v in webcolors.CSS3_HEX_TO_NAMES.items():
                    r1, g1, b1 = webcolors.hex_to_rgb(k)
                    distances[(
                        (r0 - r1)**2 + (g0 - g1)**2 + (b0 - b1)**
                        2)] = v  # Ignore sqrt as it has no significant effect
                nearest = distances[min(distances.keys())]
            return nearest
Exemplo n.º 6
0
def get_color_name(requested_color):
    """Get the exact or closest color name of an 8 bit RGB tuple."""
    try:
        color_name = webcolors.rgb_to_name(requested_color)
    except ValueError:
        color_name = closest_color(requested_color)
    return color_name
Exemplo n.º 7
0
def get_colour_name(requested_colour):
    try:
        actual_name = webcolors.rgb_to_name(requested_colour)
        return actual_name
    except ValueError:
        closest_name = closest_colour(requested_colour)
        return closest_name
Exemplo n.º 8
0
 def get_name(cls, value):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(value)
     except ValueError:
         closest_name = cls.closest_color(value)
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 9
0
def getColorName(requestedColor):
    try:
        closestName = actualName = webcolors.rgb_to_name(requestedColor)
    except ValueError:
        closestName = closestColor(requestedColor)
        actualName = None
    return actualName, closestName
 def get_colour_name(self):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(self.rgb)
     except ValueError:
         closest_name = self.closest_colour()
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 11
0
 def get_color(color):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(color)
     except:
         closest_name = closest_color(color)
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 12
0
def color_naming(center):
    try:
                actual_color=webcolors.rgb_to_name(tuple(center))
                color_names=str(actual_color)
    except ValueError:
                d={}
                for (key,name_color) in webcolors.css3_hex_to_names.items():
                    r_w,g_w,b_w=webcolors.hex_to_rgb(key)
                    r,g,b=(int(center[0]),int(center[1]),int(center[2]))
                    #calculates hsv values
                    lib_lab=np.uint8(np.asarray([[list((r_w,g_w,b_w))]]))
                    lib_lab=cv2.cvtColor(lib_lab,cv2.COLOR_RGB2Lab)

                    given_lab=np.uint8(np.asarray([[list((r,g,b))]]))
                    given_lab=cv2.cvtColor(given_lab,cv2.COLOR_RGB2Lab)

                    #Extracting individual l,a,b values
                    r_w,g_w,b_w=((lib_lab[0][0])[0],(lib_lab[0][0])[1],(lib_lab[0][0])[2])
                    r,g,b=((given_lab[0][0])[0],(given_lab[0][0])[1],(given_lab[0][0])[2])
                    #r,g,b=colorsys.rgb_to_hsv(center[0]/255.0,center[1]/255.0,center[2]/255.0)
                    #calculate mean squared error  
                    #r=(int(r_w)-int(r))**2
                    g=(int(g_w)-int(g))**2
                    b=(int(b_w)-int(b))**2
                    d[r+g+b]=name_color
                #print(d)
                color_names=d[min(d.keys())]
    return color_names
Exemplo n.º 13
0
def get_color(color): 
    try:
         closestname = actualname = webcolors.rgb_to_name(color)
    except ValueError: 
        closestname = closest_colors(color)
        actualname = None
    return actualname, closestname
Exemplo n.º 14
0
def get_colour_name(requested_colour):
    try:
        colour_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        colour_name = closest_colour(requested_colour)

    return colour_name
 def get_colour_name(self):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(self.rgb)
     except ValueError:
         closest_name = self.closest_colour()
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 16
0
    def detect_properties(self, path):
        """Detects image properties in the file."""
        from google.cloud import vision
        client = vision.ImageAnnotatorClient()

        with io.open(path, 'rb') as image_file:
            content = image_file.read()

        image = vision.types.Image(content=content)

        response = client.image_properties(image=image)
        props = response.image_properties_annotation
        #print('Properties:')

        for color in props.dominant_colors.colors:
            #print('fraction: {}'.format(color.pixel_fraction))
            #print('\tr: {}'.format(color.color.red))
            #print('\tg: {}'.format(color.color.green))
            #print('\tb: {}'.format(color.color.blue))
            #print('\ta: {}'.format(color.color.alpha))
            try:
                closest_name = actual_name = webcolors.rgb_to_name(
                    (int(color.color.red), int(
                        color.color.green), int(color.color.blue)),
                    spec=u'html4')
                actual_name = self.get_basic_colors(actual_name)
                self.colors.append(actual_name)
            except ValueError:
                closest_name = self.closest_colour(
                    (int(color.color.red), int(color.color.green),
                     int(color.color.blue)))
                closest_name = self.get_basic_colors(closest_name)
                self.colors.append(closest_name)
        return self.colors
Exemplo n.º 17
0
 def getColorName(self, pixel):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(pixel)
     except ValueError:
         closest_name = self.closestColor(pixel)
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 18
0
def get_colour_name(requested_colour):
    """Outputs closest english color name to input RGB"""
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name
Exemplo n.º 19
0
def get_color_name(requested_color):
    """ Renvoyer le nom de la couleur CSS la plus proche """
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_color)
    except ValueError:
        closest_name = closest_color(requested_color)
        actual_name = None
    return actual_name or "&#8776;{}".format(closest_name)
Exemplo n.º 20
0
def get_colour_name(requested_colour):
    try:
        closest_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        print "Color name not found for", requested_colour, ". Closest found: ", closest_name

    return closest_name
Exemplo n.º 21
0
def get_colour_name(requested_colour):
    """Given tuple (r,g,b) returns nearest CSS 2.1 color name"""
    try:
        actual_name = webcolors.rgb_to_name(requested_colour, spec="css21")
        return actual_name
    except ValueError:
        closest_name = closest_colour(requested_colour)
        return closest_name
Exemplo n.º 22
0
 def get_colour_name(self,requested_colour):
     try:
         closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
     except ValueError:
         closest_name = self.closest_colour(requested_colour)
         actual_name = None
     # print("get_colour_name")
     return actual_name, closest_name
Exemplo n.º 23
0
def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = wb.rgb_to_name(requested_colour,
                                                    spec=u'html4')
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name
Exemplo n.º 24
0
def get_colour_name(requested_colour):
    """Given tuple (r,g,b) returns nearest CSS 2.1 color name"""
    try:
        actual_name = webcolors.rgb_to_name(requested_colour,spec='css21')
        return actual_name
    except ValueError:
        closest_name = closest_colour(requested_colour)
        return closest_name
Exemplo n.º 25
0
 def get_colour_name(dominant_color):
     try:
         actual_name = webcolors.rgb_to_name(dominant_color, spec='css3')
         closest_name = None
     except ValueError:
         closest_name = closest_colour(dominant_color)
         actual_name = None
     return actual_name, closest_name
Exemplo n.º 26
0
	def color_tuple_to_string(rgb):
		# try to convert to an english name
		try:
			return webcolors.rgb_to_name(rgb)
		except Exception as e:
			#print e
			pass
		return str(rgb)
Exemplo n.º 27
0
    def get_colour_name(self, requested_colour):
        """ get a name match for the closest colour"""
        try:
            closest_name = webcolors.rgb_to_name(requested_colour)
        except ValueError:
            closest_name = self.closest_colour(requested_colour)

        return closest_name
Exemplo n.º 28
0
def rgb_to_css_color(rgb):
    # rgb to css color uses webcolor to see if there is a named
    # css color equivilent to the rgb color passed in, and if not
    # returns a string in the form "rgb(1,2,3)"
    try:
        return webcolors.rgb_to_name(rgb)
    except ValueError:
        return "rgb" + str(rgb)
Exemplo n.º 29
0
	def color_tuple_to_string(rgb):
		# try to convert to an english name
		try:
			return webcolors.rgb_to_name(rgb)
		except Exception as e:
			#print e
			pass
		return str(rgb)
def get_colour_name(requested_colour):
    requested_colour = [int(s) for s in requested_colour]
    #print("$$$$$$$$$",requested_colour)
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return closest_name
Exemplo n.º 31
0
    def test_rgb_to_name(self):
        """
        Test conversion from integer RGB triplet to color name.
        """
        test_pairs = (((255, 255, 255), u'white'), ((0, 0, 128), u'navy'),
                      ((218, 165, 32), u'goldenrod'))

        for triplet, name in test_pairs:
            self.assertEqual(name, webcolors.rgb_to_name(triplet))
Exemplo n.º 32
0
def get_color_name(requested_color):
    ''' Get the name of a color (either according to CSS3 or CSS2.1). If exact color cannot be mapped, this method finds the closest color. '''

    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_color)
    except ValueError:
        closest_name = closest_color(requested_color)
        actual_name = None
    return closest_name
 def get_colour_name(requested_colour):
     try:
         closest_name = webcolors.rgb_to_name(requested_colour)
     except ValueError:
         try:
             closest_name = closest_colour(requested_colour)
         except:
             closest_name = np.nan
     return closest_name
Exemplo n.º 34
0
def get_colour_name(requested_colour):
    """
        Return the name of the color
    """
    try:
        closest_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
    return closest_name
Exemplo n.º 35
0
def get_colour_name(rgb):
    requested_colour = (rgb.r, rgb.g, rgb.b)
    hex_code = rgb2hex(rgb.r, rgb.g, rgb.b)
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name, hex_code
def get_colour_name(requested_colour):
    """
    docstring
    """
    try:
        closest_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
    return closest_name
Exemplo n.º 37
0
def get_color_name(requested_color):
    ''' Get the name of a color (either according to CSS3 or CSS2.1). If exact color cannot be mapped, this method finds the closest color. '''

    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_color)
    except ValueError:
        closest_name = closest_color(requested_color)
        actual_name = None
    return closest_name
def get_colour_name(requested_colour):
    try:
        #print("We are trying")
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        #print("We are in the exception")
        closest_name = closest_colour(requested_colour)
        # print(closest_name)
        actual_name = None
    return actual_name, closest_name
Exemplo n.º 39
0
def get_colour_name(colour):
    '''Translate given RGB colour into it's name.
    If name cannot be found, return closest known colour.
    '''
    try:
        name = webcolors.rgb_to_name(colour)
    except ValueError:
        name = get_closest_colour(colour)

    return name
def get_colour_name(requested_colour):
	'''
	function that finds the colour that matches input RGB code.
	If no matching colour is found, call function closest_colour
	'''
	try:
		closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
	except ValueError:
		closest_name = closest_colour(requested_colour)
		actual_name = None
	return actual_name, closest_name
Exemplo n.º 41
0
    def test_rgb_to_name(self):
        """
        Test conversion from integer RGB triplet to color name.
        """
        test_pairs = (((255, 255, 255), u'white'),
                      ((0, 0, 128), u'navy'),
                      ((218, 165, 32), u'goldenrod'))

        for triplet, name in test_pairs:
            self.assertEqual(name,
                             webcolors.rgb_to_name(triplet))
Exemplo n.º 42
0
def _get_color_names(array):
    """Use webcolors to get the closest named color for rgb values"""
    import webcolors as wb
    array = np.atleast_2d(array)
    color_names = []
    for rgb in array:
        try:
            i_name = wb.rgb_to_name(rgb)
        except ValueError:
            i_name = _closest_color_from_rgb(rgb)
        color_names.append(i_name)
    return color_names
Exemplo n.º 43
0
        def paint_picture(x, y):
            while y < self.img.size[1]:
                pixel = pixor[x,y]
                color = webcolors.rgb_to_name(pixel)
                space = get_space(x,y, color)

                string = check_fit(space, color)
                drawn = self.texter.render(string, True, pixel)
                self.synpic.blit(drawn, (x * letWidth, y * letHeight))
                x += (len(string))
                if x >= self.img.size[0]:
                    y += 1
                    x = 0
Exemplo n.º 44
0
def BGRname(bgrcolor):
    print "rgb = ", bgrcolor[2], bgrcolor[1], bgrcolor[0]
    try:
        closest_name = webcolors.rgb_to_name((bgrcolor[2], bgrcolor[1], bgrcolor[0]))
    except ValueError:
        min_colors = {}
        for key, name in webcolors.css21_hex_to_names.items():
            r_c, g_c, b_c = webcolors.hex_to_rgb(key)
            bd = (b_c - bgrcolor[0]) ** 2
            gd = (g_c - bgrcolor[1]) ** 2
            rd = (r_c - bgrcolor[2]) ** 2
            min_colors[(rd + gd + bd)] = name
        closest_name =  min_colors[min(min_colors.keys())]
    return closest_name
Exemplo n.º 45
0
    def test_rgb_to_name_specs(self):
        """
        Using one of the supported specifications succeeds; an
        unsupported specification raises ValueError.
        """
        for supported_spec in (u'html4', u'css2', u'css21', u'css3'):
            self.assertEqual(u'white',
                             webcolors.rgb_to_name((255, 255, 255),
                                                   spec=supported_spec))

        for unsupported_spec in (u'css1', u'css4', u'html5'):
            self.assertRaises(ValueError,
                              webcolors.rgb_to_name,
                              (255, 255, 255), spec=unsupported_spec)
Exemplo n.º 46
0
    def get_closest(hex):

        rgb = webcolors.hex_to_rgb(hex)

        try:
            closest_name = actual_name = webcolors.rgb_to_name(rgb)
        except ValueError:
            closest_name = closest_colour(rgb)
            actual_name = None

        if actual_name:
            actual = webcolors.name_to_hex(actual_name)
        else:
            actual = None

        closest = webcolors.name_to_hex(closest_name)

        return actual, closest
Exemplo n.º 47
0
    def test_rgb_to_name_specs(self):
        """
        Using one of the supported specifications succeeds; an
        unsupported specification raises ValueError.

        """
        for supported_spec in webcolors.SUPPORTED_SPECIFICATIONS:
            result = webcolors.rgb_to_name(
                (255, 255, 255), spec=supported_spec
            )
            assert u'white' == result

        for unsupported_spec in (u'css1', u'css4', u'html5'):
            self.assertRaises(
                ValueError,
                webcolors.rgb_to_name,
                (255, 255, 255),
                spec=unsupported_spec
            )
Exemplo n.º 48
0
 def get_space(x,y, color):
     x1 = x
     while x < self.img.size[0] and webcolors.rgb_to_name(pixor[x,y]) is color:
         x += 1
     return x - x1
Exemplo n.º 49
0
def loop():
    #img=s.Image(r"C:\Users\Abhas_2\Desktop\untitled.png").scale(.25)
    img=cam.getImage()
    #clock1=time.clock()
    #print("0: "+str(1/(time.clock()-clock1)))
    img2=img.toGray()
    #print("1: "+str(1/(time.clock()-clock1)))
    img_black=img2.stretch(50,50)
    #img_black=img2.threshold(10)
    #print("2: "+str(1/(time.clock()-clock1)))
    bot_blobs=img_black.invert().findBlobs()
    if bot_blobs:
        bot=bot_blobs[-1:][0]
        #print("3: "+str(1/(time.clock()-clock1)))
        bot_hole=(img2.crop(bot)-bot.hullMask().invert()).morphOpen()
        #print("4: "+str(1/(time.clock()-clock1)))
        hole_blobs=bot_hole.findBlobs()
        if hole_blobs:
            hole=hole_blobs[-1:][0]
        hole_center=[bot.topLeftCorner()[0]+hole.x, bot.topLeftCorner()[1]+hole.y]
        #print("5: "+str(1/(time.clock()-clock1)))
        bot_center=bot.centroid()
        bot_radius=bot.radius()
        bot_angle=ball_angle=degrees(atan2(-(hole_center[1]-bot_center[1]),hole_center[0]-bot_center[0]))
        img3=(img2-img)+(img-img2)
        #img3.show()

        #f[0].show()
        #print("6: "+str(1/(time.clock()-clock1)))
        img_=img-img3.threshold(5).invert()
        #print("7: "+str(1/(time.clock()-clock1)))
        f=img_.findBlobs()
        #print("8: "+str(1/(time.clock()-clock1)))
        if f:
            f=f[-1:]
            c_match=f[0].meanColor()
            ball_centre=[f[0].x,f[0].y]
            ball_angle=degrees(atan2(-(ball_centre[1]-bot_center[1]),ball_centre[0]-bot_center[0]))
            #print(ball_angle)
            img.drawLine(ball_centre,bot_center,s.Color.YELLOW,5)
            #img_.drawLine(bot_center,[bot_center[0]+cos(radians( bot_angle))*bot_radius,bot_center[1]-sin(radians(bot_angle))*bot_radius],s.Color.BLUE,5)
            #img_.show()
            #c=f[0].centroid()
            #out=img_.crop(c[0],c[1],f[0].width(),f[0].height(),True)

            #img_in=out.crop(out.width/2,out.height/2,out.width/sqrt(2),out.height/sqrt(2),True)
            #c_match=img_in.toRGB().meanColor()
            #print(c_match)
            c_match=s.Color.getHueFromRGB(c_match)
            #print(c_match)
            least=255*3
            color=None
            list_check=s.Color.colorlist
            #list_check=[s.Color.getHueFromRGB(s.Color.YELLOW),s.Color.getHueFromRGB(s.Color.RED),s.Color.getHueFromRGB(s.Color.BLUE),s.Color.getHueFromRGB(s.Color.GREEN),s.Color.getHueFromRGB(s.Color.HOTPINK)]
            for c in list_check:
                c_=s.Color.getHueFromRGB(c)
                diff=abs(c_match-c_)
                if diff<least:
                    #print(diff)
                    #print(c_)
                    color=c_
                    least=diff
            #print(color)
            color=s.Color.hueToRGB(color)
            #print(color)

            color_name=w.rgb_to_name(color)
            #print(color_name)
        #print("9: "+str(1/(time.clock()-clock1)))
        img.drawLine([bot.x,bot.y],hole_center,s.Color.AQUAMARINE,thickness=5)

        #minimum angle between ball_angle and bot_angle
        # returns positive angle for anti-clockwise rotation and else negative angle
        diff_angle = ball_angle-bot_angle
        diff_angle = (diff_angle + 180) % 360 - 180
        print(diff_angle)

        #send_signal(diff_angle)
        #print("10: "+str(1/(time.clock()-clock1)))
        img.show()
Exemplo n.º 50
0
 def rgb_to_name_or_hex(rgb):
     try:
         return webcolors.rgb_to_name(rgb)
     except ValueError as e:
         pass
     return webcolors.rgb_to_hex(rgb)
Exemplo n.º 51
0
def convert_color(bgr):
    try:
        b, g, r = bgr
        return webcolors.rgb_to_name((r, g, b))
    except ValueError:
        return closest_color(bgr)
Exemplo n.º 52
0
def get_colour_name(rgb, palette="css3"):
    try:
        colorname = rgb_to_name(rgb, spec=palette)
        return colorname
    except:
        return "Undefined"