title: How are floating-point pixel values converted to integer values?
body: <p>How does image library (such as PIL, OpenCV, etc) convert floating-point values to integer pixel values? </p>
  
  <p>For example </p>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  from PIL import Image
  
  # Creates a random image and saves in a file
  def get_random_img(m=0, s=1, fname='temp.png'):
  
      im =  m + s * np.random.randn(60, 60, 3) # For eg. min: -3.8947058634971179, max: 3.6822041760496904
  
      print(im[0, 0]) # for eg. array([ 0.36234732, 0.96987366, 0.08343])
  
      imp = Image.fromarray(im, 'RGB') #                         (*)
  
      print(np.array(imp)[0, 0]) # [140 , 74, 217] 
  
      imp.save(fname) 
  
      return im, imp
  
  </code></pre>
  
  <p>For the above method, an example is provided in the comment (which is randomly produced). My question is: <strong>how</strong> does <code>(*)</code> convert <code>ndarray</code> (which can range from - infinity to plus infinity) to pixel values between 0 and 255? </p>
  
  <p>I tried to investigate the <code>Pil.Image.fromarray</code> method and eventually ended by at line #798 <code>d.decode(data)</code> within <code>Pil.Image.Image().frombytes</code> method. I could find the implementation of <code>decode</code> method, thus unable to know what computation goes behind the conversion. </p>
  
  <p>My initial thought was that maybe the method use <strong>minimum</strong> (to 0) and <strong>maximum</strong> (to 255) value from the array and then map all the other values accordingly between 0 and 255. But upon investigations, I found out that's not what is happening. Moreover, how does it handle when the values of the array range between 0 and 1 or any other range of values?</p>