Reputation: 912
I am working on a project and I am trying to find the different colours used in an image, I have a list of Hex colours and trying to convert them to colour names. I know that not all the hexdecimals have color names related to them but I have found a huge dictionary with the names and I am trying to link them together.
I am trying to convert the Hex colour to the equivalent name in this dictionary how to do that?
import pandas as pd
from PIL import Image
from collections import Counter
img = Image.open("bnw.jpg")
size = w, h = img.size
data = img.load()
colors = []
for x in range(w):
for y in range(h):
color = data[x, y]
hex_color = '#'+''.join([hex(c)[2:].rjust(2, '0') for c in color])
colors.append(hex_color)
total = w * h
color_hex = []
color_count = []
color_percent = []
df = pd.DataFrame()
for color, count in Counter(colors).items():
percent = count/total * \
100 # Do not make it int. Majority of colors are < 1%, unless you want >= 1%
if percent > 1:
color_hex.append(color)
color_count.append(count)
color_percent.append(percent)
df['color'] = color_hex
df['count'] = color_count
df['percent'] = color_percent
df.to_excel(r'C:\Users\User\Desktop\Project\export_dataframe.xlsx',
index=False, header=True)
print('done')
here is the dictionary (this is half of it)
colors_dict = {
"0048BA": "Absolute Zero", "B0BF1A": "Acid green", "7CB9E8": "Aero", "C9FFE5": "Aer o blue", "B284BE": "African violet", "72A0C1": "Air superiority blue", "EDEAE0": "Alabaster", "F0F8FF": "Alice blue", "C46210": "Alloy orange", "EFDECD": "Almond", "E52B50": "Amaranth", "9F2B68": "Amaranth (M&P)", "F19CBB": "Amaranth pink", "AB274F": "Amaranth purple", "D3212D": "Amaranth red", "3B7A57": "Amazon", "FFBF00": "Amber", "FF7E00": "Amber (SAE/ECE)", "9966CC": "Amethyst", "A4C639": "Android green", "CD9575": "Antique brass", "665D1E": "Antique bronze", "915C83": "Antique fuchsia", "841B2D": "Antique ruby", "FAEBD7": "Antique white", "008000": "Ao (English)",
"8DB600": "Apple green", "FBCEB1": "Apricot", "00FFFF": "Aqua", "7FFFD4": "Aquamarine", "D0FF14": "Arctic lime", "4B5320": "Army green", "8F9779": "Artichoke", "E9D66B": "Arylide yellow", "B2BEB5": "Ash gray", "87A96B": "Asparagus", "FF9966": "Atomic tangerine", "A52A2A": "Auburn", "FDEE00": "Aureolin", "568203": "Avocado", "007FFF": "Azure", "F0FFFF": "Azure (X11/web color)", "89CFF0": "Baby blue", "A1CAF1": "Baby blue eyes", "F4C2C2": "Baby pink", "FEFEFA": "Baby powder", "FF91AF": "Baker-Miller pink", "FAE7B5": "Banana Mania", "DA1884": "Barbie Pink", "7C0A02": "Barn red", "848482": "Battleship grey", "BCD4E6": "Beau blue",
"9F8170": "Beaver", "F5F5DC": "Beige", "2E5894": "B'dazzled blue", "9C2542": "Big dip o’ruby", "FFE4C4": "Bisque", "3D2B1F": "Bistre", "967117": "Bistre brown", "CAE00D": "Bitter lemon", "BFFF00": "Bitter lime", "FE6F5E": "Bittersweet", "BF4F51": "Bittersweet shimmer", "000000": "Black", "3D0C02": "Black bean", "1B1811": "Black chocolate", "3B2F2F": "Black coffee", "54626F": "Black coral", "3B3C36": "Black olive", "BFAFB2": "Black Shadows", "FFEBCD": "Blanched almond", "A57164": "Blast-off bronze", "318CE7": "Bleu de France", "ACE5EE": "Blizzard blue", "FAF0BE": "Blond", "660000": "Blood red", "0000FF": "Blue", "1F75FE": "Blue (Crayola)",
"0093AF": "Blue (Munsell)", "0087BD": "Blue (NCS)", "0018A8": "Blue (Pantone)", "333399": "Blue (pigment)", "0247FE": "Blue (RYB)", "A2A2D0": "Blue bell", "6699CC": "Blue-gray", "0D98BA": "Blue-green", "064E40": "Blue-green (color wheel)", "5DADEC": "Blue jeans", "126180": "Blue sapphire", "8A2BE2": "Blue-violet", "7366BD": "Blue-violet (Crayola)", "4D1A7F": "Blue-violet (color wheel)", "5072A7": "Blue yonder", "3C69E7": "Bluetiful", "DE5D83": "Blush", "79443B": "Bole", "E3DAC9": "Bone", "006A4E": "Bottle green", "87413F": "Brandy", "CB4154": "Brick red", "66FF00": "Bright green", "D891EF": "Bright lilac", "C32148": "Bright maroon", "1974D2": "Bright navy blue",
"FFAA1D": "Bright yellow (Crayola)", "FF55A3": "Brilliant rose", "FB607F": "Brink pink", "004225": "British racing green", "CD7F32": "Bronze", "88540B": "Brown", "AF6E4D": "Brown sugar", "1B4D3E": "Brunswick green", "7BB661": "Bud green", "F0DC82": "Buff", "800020": "Burgundy", "DEB887": "Burlywood", "A17A74": "Burnished brown", "CC5500": "Burnt orange", "E97451": "Burnt sienna", "8A3324": "Burnt umber", "BD33A4": "Byzantine", "702963": "Byzantium", "536872": "Cadet", "5F9EA0": "Cadet blue", "A9B2C3": "Cadet blue (Crayola)", "91A3B0": "Cadet grey", "006B3C": "Cadmium green", "ED872D": "Cadmium orange", "E30022": "Cadmium red", "FFF600": "Cadmium yellow",
"A67B5B": "Café au lait", "4B3621": "Café noir", "A3C1AD": "Cambridge blue", "C19A6B": "Camel", "EFBBCC": "Cameo pink", "FFFF99": "Canary", "FFEF00": "Canary yellow", "FF0800": "Candy apple red", "E4717A": "Candy pink", "00BFFF": "Capri", "592720": "Caput mortuum", "C41E3A": "Cardinal", "00CC99": "Caribbean green", "960018": "Carmine", "D70040": "Carmine (M&P)", "FFA6C9": "Carnation pink", "B31B1B": "Carnelian", "56A0D3": "Carolina blue", "ED9121": "Carrot orange", "00563F": "Castleton green", "703642": "Catawba", "C95A49": "Cedar Chest", "ACE1AF": "Celadon", "007BA7": "Celadon blue", "2F847C": "Celadon green", "B2FFFF": "Celeste",
"246BCE": "Celtic blue", "DE3163": "Cerise", "007BA7": "Cerulean", "2A52BE": "Cerulean blue", "6D9BC3": "Cerulean frost", "1DACD6": "Cerulean (Crayola)", "007AA5": "CG blue", "E03C31": "CG red", "F7E7CE": "Champagne", "F1DDCF": "Champagne pink", "36454F": "Charcoal", "232B2B": "Charleston green", "E68FAC": "Charm pink", "DFFF00": "Chartreuse (traditional)", "7FFF00": "Chartreuse (web)", "FFB7C5": "Cherry blossom pink", "954535": "Chestnut", "DE6FA1": "China pink", "A8516E": "China rose", "AA381E": "Chinese red", "856088": "Chinese violet", "FFB200": "Chinese yellow", "7B3F00": "Chocolate (traditional)", "D2691E": "Chocolate (web)", "FFA700": "Chrome yellow", "98817B": "Cinereous",
"E34234": "Cinnabar", "CD607E": "Cinnamon Satin", "E4D00A": "Citrine", "9FA91F": "Citron", "7F1734": "Claret", "0047AB": "Cobalt blue", "D2691E": "Cocoa brown", "6F4E37": "Coffee", "B9D9EB": "Columbia Blue", "F88379": "Congo pink", "8C92AC": "Cool grey", "B87333": "Copper", "DA8A67": "Copper (Crayola)", "AD6F69": "Copper penny", "CB6D51": "Copper red", "996666": "Copper rose", "FF3800": "Coquelicot", "FF7F50": "Coral", "F88379": "Coral pink", "893F45": "Cordovan", "FBEC5D": "Corn", "6495ED": "Cornflower blue", "FFF8DC": "Cornsilk", "2E2D88": "Cosmic cobalt", "FFF8E7": "Cosmic latte", "81613C": "Coyote brown",
"FFBCD9": "Cotton candy", "FFFDD0": "Cream", "DC143C": "Crimson", "9E1B32": "Crimson (UA)", "F5F5F5": "Cultured", "00FFFF": "Cyan", "00B7EB": "Cyan (process)", "58427C": "Cyber grape", "FFD300": "Cyber yellow", "F56FA1": "Cyclamen", "666699": "Dark blue-gray", "654321": "Dark brown", "5D3954": "Dark byzantium", "26428B": "Dark cornflower blue", "008B8B": "Dark cyan", "536878": "Dark electric blue", "B8860B": "Dark goldenrod", "013220": "Dark green", "006400": "Dark green (X11)", "1A2421": "Dark jungle green", "BDB76B": "Dark khaki", "483C32": "Dark lava", "534B4F": "Dark liver", "543D37": "Dark liver (horses)", "8B008B": "Dark magenta", "4A5D23": "Dark moss green",
"556B2F": "Dark olive green", "FF8C00": "Dark orange", "9932CC": "Dark orchid", "03C03C": "Dark pastel green", "301934": "Dark purple", "8B0000": "Dark red", "E9967A": "Dark salmon", "8FBC8F": "Dark sea green", "3C1414": "Dark sienna", "8CBED6": "Dark sky blue", "483D8B": "Dark slate blue", "2F4F4F": "Dark slate gray", "177245": "Dark spring green", "00CED1": "Dark turquoise", "9400D3": "Dark violet", "00703C": "Dartmouth green", "555555": "Davy's grey", "DA3287": "Deep cerise", "FAD6A5": "Deep champagne", "B94E48": "Deep chestnut", "004B49": "Deep jungle green", "FF1493": "Deep pink", "FF9933": "Deep saffron", "00BFFF": "Deep sky blue", "4A646C": "Deep Space Sparkle", "7E5E60": "Deep taupe",
"1560BD": "Denim", "2243B6": "Denim blue", "C19A6B": "Desert", "EDC9AF": "Desert sand", "696969": "Dim gray", "1E90FF": "Dodger blue", "D71868": "Dogwood rose", "967117": "Drab", "00009C": "Duke blue", "EFDFBB": "Dutch white", "E1A95F": "Earth yellow", "555D50": "Ebony", "C2B280": "Ecru", "1B1B1B": "Eerie black", "614051": "Eggplant", "F0EAD6": "Eggshell", "1034A6": "Egyptian blue", "7DF9FF": "Electric blue", "00FF00": "Electric green", "6F00FF": "Electric indigo", "CCFF00": "Electric lime", "BF00FF": "Electric purple", "8F00FF": "Electric violet", "50C878": "Emerald", "6C3082": "Eminence", "1B4D3E": "English green"}
Upvotes: 1
Views: 641
Reputation: 1319
You can use .apply() method for Series and .get() method for dictionaries, something like this:
df_image['color_name'] = df_image['color'].apply(lambda c: colors_dict.get(c, 'Not Found')) # If color isn't in dictionary, then we set 'Not found'.
Output:
color count percent color_name
0 0048BA 1 0.55 Absolute Zero
1 FFAA1D 4 0.43 Bright yellow (Crayola)
2 FFBCD9 6 0.21 Cotton candy
3 556B2F 8 0.73 Dark olive green
4 4N0TH3R 8 0.34 Not Found
Upvotes: 1