Reputation: 13
I need to print a sort of 'background' of random pixels but only choose the colors from a given list. example of image
I'm using this code to print the image:
import numpy as np
import matplotlib.pyplot as plt
img = (np.random.standard_normal([28, 28, 3]) * 255).astype(np.uint8)
plt.imshow(img)
But I'm having trouble implementing the choice of colors from the list in the img 'array', either in hex or RGB representation. Any solution will do, not necessarily in matplotlib. Thanks!
Upvotes: 1
Views: 187
Reputation: 2167
As I understand you need predefined colors list:
import numpy as np
import matplotlib.pyplot as plt
import random
colors = [
(1.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(0.0, 0.0, 1.0),
(1.0, 1.0, 0.0),
]
img = np.zeros(shape=(28, 28, 3))
# print(img)
for i in range(28):
for j in range(28):
img[i, j] = random.choice(colors)
plt.imshow(img)
Upvotes: 1
Reputation: 1
I can't see where you're implementing the list, furthermore standard_normal gives you values in [-1,1] so after casting them to uint8 they're still technically random and in the right range but...
Is this what you're looking for?
import numpy as np
import matplotlib.pyplot as plt
numberOfColors = 10
imgShape = [28,28,3]
colorGenerator = lambda channels : [ 127*(np.random.standard_normal([channels]) + 1.0)]
#generate a list of possible colours
colorList = np.array([colorGenerator[channels] for i in range(numberOfColors)],dtype=np.uint8)
#pick a colour from the list for each pixel
indices = np.random.randint(0,numberOfColors,size=[imgShape[0],imgShape[1]]).astype(np.int)
img = colorList[indices].reshape(imgShape)
plt.imshow(img)
plt.show()
Upvotes: 0
Reputation: 33
you can choose randomly from a list:
import random
colors = ['#AABBCC', '#FFFF00', '#AA00AA']
random_index = random.randint(0, len(colors)-1)
# then you can access the random chosen color like this:
colors[random_index]
Upvotes: 0