title: Problems converting image data to unsigned int types for use with OpenCV
body: <p>I have an 8-bit <code>.gif</code> image that I'm able to read into a Numpy array of dtype <code>uint8</code>. However, when I try to convert the dtype to a larger type of integer, such as <code>uint32</code> and <code>uint64</code>, for use in OpenCV, I get weird errors.</p>
  <p>I'm using Google Colab and am running Python 3.6.</p>
  <p>I downloaded this 8-bit color gif image:</p>
  <pre><code>!wget https://www.cosy.sbg.ac.at/~pmeerw/Watermarking/lena_color.gif
  </code></pre>
  <p>And here's my code below where I try to convert the image to each of <code>[ np.uint8, np.uint16, np.uint32, np.uint64 ]</code> and then draw a rectangle.</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import cv2
  import matplotlib
  
  print(np.__version__)          # 1.19.5
  print(cv2.__version__)         # 4.1.2
  print(matplotlib.__version__)  # 3.2.2
  
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  
  
  img_orig = mpimg.imread('lena_color.gif')
  print('img_orig dtype: %s' % img_orig.dtype) # img_orig dtype: uint8
  
  # Coordinates for rectangle to be drawn.
  TL = (100, 100)
  BR = (400, 400)
  
  # Convert to different integer dtypes.
  dtypes = [ np.uint8, np.uint16, np.uint32, np.uint64 ]
  
  for dtype in dtypes:
  
      print('Changing type to %s' % (dtype.__name__))
  
      img = img_orig.astype(dtype)
      try:
          cv2.rectangle(img, TL, BR, (0, 0, 255), 2)
          _ = plt.imshow(img)
          _ = plt.show()
      except Exception as ex:
          print(ex)
  </code></pre>
  <p>I get the following results:</p>
  <pre><code>Changing type to uint8
  </code></pre>
  <p><a href="https://i.stack.imgur.com/iGRy0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iGRy0.png" alt="enter image description here" /></a></p>
  <pre><code>Changing type to uint16
  </code></pre>
  <p><a href="https://i.stack.imgur.com/jyLHS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jyLHS.png" alt="enter image description here" /></a></p>
  <pre><code>Changing type to uint32
  an integer is required (got type tuple)
  </code></pre>
  <pre><code>Changing type to uint64
  </code></pre>
  <p><a href="https://i.stack.imgur.com/dwGjf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dwGjf.png" alt="enter image description here" /></a></p>
  <p><strong>Specific questions</strong>:</p>
  <ol>
  <li><p>Why do I get an exception when I change the dtype to <code>uint32</code> ?</p>
  </li>
  <li><p>I drew the rectangle with blue <code>(0, 0, 255)</code>. Why is the rectangle coming out as kind of white-purple?</p>
  </li>
  <li><p>Why does the rectangle not appear when the dtype is <code>uint64</code> ?</p>
  </li>
  </ol>