1

Using OpenCv I tried to do a simple color-palette by getting RGB values from User. I created a black image using numpy and changed the whole image with user entered BGR values and tried to display it.Unfortunately Python and Ipython stops responding

import cv2
import numpy as np

while(1):

    #Getting RGB values from user

    r=int(input("Enter R value"))
    g=int(input("Enter G value"))
    b=int(input("Enter B value"))

    #Making a Black image
    img=np.zeros((520,520,3),np.uint8)

    #Changing whole image with user defined RGB values
    img[:]=[b,g,r]

    #Making the Image to display
    cv2.imshow("Pallete",img)

    #Breaking the process while pressing ESC key
    key=cv2.waitKey(1)&0xFF
    if(key==27):
        break

    #My IPython Window is not Responding and gets strucked
  • 1
    OpenCV displays don't work very well with Jupyter/IPython. Instead, I'd suggest using Matplotlib to show the images. Note that Matplotlib displays images in RGB, not BGR order. – alkasm Jul 20 '17 at 07:19
  • It did work for previous python codes but struggles to open only for this program. That's my problem. Is there any error in code? or changes needed? @AlexanderReynolds – Sundaramoorthy Anandh Jul 20 '17 at 13:11
  • `waitKey(1)` will draw the image and the loop will start over again and it gives you a tiny 1ms window to press the key stroke before it decides to move on. Use a longer time. Just use `waitKey(0)` to wait indefinitely for a key-press. – alkasm Jul 20 '17 at 13:17
  • WhoohoooYeah! That works a lot prettier. Thanks :) – Sundaramoorthy Anandh Jul 20 '17 at 13:22
  • But my key value always holds value 255 eventhough I press different keys ! why? – Sundaramoorthy Anandh Jul 20 '17 at 13:26
  • That's a different question. First, [read this](https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1). Then, post your code that is giving you a result of 255 with every key press. – alkasm Jul 20 '17 at 13:32
  • I know that 'cv2.waitKey(0) & 0xFF == ord(any_key)' will waits for the particular key to press and do the following operations. But my question is the value stored in the variable is 255 always! – Sundaramoorthy Anandh Jul 20 '17 at 13:40
  • key=cv2.waitKey(0)&0xFF print('key:',key) if(key in range(0,127)): break – Sundaramoorthy Anandh Jul 20 '17 at 13:42
  • I don't know, it could be something with Jupyter. Try removing the bitwise operation. Otherwise maybe you can just use `waitKey(0)` and not store it and check? – alkasm Jul 20 '17 at 14:02

0 Answers0