简体   繁体   中英

*** ValueError: invalid literal for int() with base 10: '#'

l would like to convert my array of colors to RGB pixel values .

colors

array(['#32CD32', '#CD5C5C', '#00BFFF', '#1E90FF', '#00008B', '#ADFF2F',
       '#B0E0E6', '#7CFC00', '#00008B', '#1E90FF', '#F08080', '#F08080',
       '#FA8072', '#0000FF', '#7CFC00', '#B0E0E6'],
      dtype='<U7')

What l have tried ?

pixel_color = ['#%02x%02x%02x' % (c[0], c[1], c[2]) for c in colors]

l got the following error :

***** TypeError: %x format: an integer is required, not str**

Then l did the following :

pixel_color = ["#%02x%02x%02x" %(int(c[0]), int(c[1]), int(c[2])) for c in colors]

Then l get the following error :

***** ValueError: invalid literal for int() with base 10: '#'**

You aren't stripping the # from your input before you try the conversion. Also, why don't you just use bytes.fromhex() :

x = ['#32CD32', '#CD5C5C', '#00BFFF', '#1E90FF', '#00008B', '#ADFF2F',
'#B0E0E6', '#7CFC00', '#00008B', '#1E90FF', '#F08080', '#F08080',
'#FA8072', '#0000FF', '#7CFC00', '#B0E0E6']

for i in x:
  red, green, blue = bytes.fromhex(i[1:])
  print(red, green, blue)

Output:

50 205 50
205 92 92
0 191 255
30 144 255
0 0 139
173 255 47
176 224 230
124 252 0
0 0 139
30 144 255
240 128 128
240 128 128
250 128 114
0 0 255
124 252 0
176 224 230

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM