Search code examples
pythonpython-3.xmatplotlibcolorsrgb

Is there a way to generate a RGB color by inputting R, G, and B values from 0 up to 255 without using the matplotlib module in python?


Is there a way to generate a color based on its RGB values rather than the following snippet? (And preferably with a more compact module than matplotlib)

from matplotlib.pyplot import scatter, axis, show
R = float(input('Insert R value: '))
G = float(input('Insert G value: '))
B = float(input('Insert B value: '))

def RGB(R,G,B):
  r = R/255
  g = G/255
  b = B/255
  scatter([0],[0],color=(r, g, b), s=1e6)
  axis('off')
  print('R =', R,'\nG =', G,'\nB =', B)
  show()

RGB(R,G,B)

By inserting the values of R=50, G=100, and B=200 the code above will return the following window, which is basically a matplotlib scatter plot with an expanded dot: Code snippet return

Matplotlib is a huge module and when I try to generate an executable using Pyinstaller the size of the executable gets really big. That's the reason why I'm asking.


Solution

  • If I'm understanding your requirements properly, how about using pillow module:

    from PIL import Image
    
    r = input('Input R value: ')
    g = input('Input G value: ')
    b = input('Input B value: ')
    
    im = Image.new('RGB', (500, 300), (r, g, b))
    im.show()
    

    which will display a rectangle window filled with specified color.