Find max and min RGB values of a pixel using PIL
<p>I have a basic algorithm for desaturating an image using the pillow library and Python 3:
- find max of a pixel's RGB values
- find min of a pixel's RGB values
- calc average: (max + min) / 2</p>

<p>How do I find the min and max red, green and blue values for each pixel? I'm completely confused! I tried this code as part of a for loop</p>

<pre><code> red = image.getextrema()
 green = image.getextrema()
 blue = image.getextrema()

 average = int( (red + green + blue) / 2 )
</code></pre>

<p>but the error returned is</p>

<p>"TypeError: unsupported operand type(s) for /: 'tuple' and 'int'"</p>

<p>The same error msg appeared when I removed the int() function.</p>

<p>Not sure if I'm barking up the wrong tree completely or only slightly off the trail. Complete novice with the pillow library and just wanting to experiment with different effects. </p>

<p><code>img.getextrema()</code> returns the tuple of <code>(min_value, max_value)</code>. In order to get the average value, you have to do:</p>

<pre><code>value = img.getextrema()
avg = sum(value)/len(value) # OR, sum(value)/2, as len will always be 2
</code></pre>

python