title: How do i prevent a segmentation fault with opencv in python
body: <p>I am trying to re-produce a simple code using opencv with python 3.9.7 on ArchLinux using gnome as a desktop environment.  I installed opencv with the command <code>pip3 install --upgrade opencv-python</code>.  My current code is shown below.</p>
  <pre><code>import cv2
  cap = cv2.VideoCapture(0)
  cap.set(3, 640)
  cap.set(4, 480)
  
  while True:
      success, img = cap.read()
      cv2.imshow(&quot;Video&quot;, img)  # This is where the code fails
      if cv2.waitKey(1) &amp; 0xFF == ord('q'):
          break
  </code></pre>
  <p>The code runs if I remove everything below the <code>imshow()</code> command, but when I add that single line, the execution fails with the following error</p>
  <p><code>Segmentation fault (core dumped)</code></p>
  <p>It appears that a number of people have had this issue, but I have not seen a resolution.  Does anyone know how I can fix this?</p>