Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume you are getting an error like.. <code>TypeError: 'PixelAccess' object is not iterable</code>...?</p> <p>See the <a href="http://effbot.org/imagingbook/image.htm#tag-Image.Image.load" rel="noreferrer">Image.load</a> documentation for how to access pixels..</p> <p>Basically, to get the list of pixels in an image, using <code>PIL</code>:</p> <pre><code>from PIL import Image i = Image.open("myfile.png") pixels = i.load() # this is not a list, nor is it list()'able width, height = i.size all_pixels = [] for x in range(width): for y in range(height): cpixel = pixels[x, y] all_pixels.append(cpixel) </code></pre> <p>That appends every pixel to the <code>all_pixels</code> - if the file is an RGB image (even if it only contains a black-and-white image) these will be a tuple, for example:</p> <pre><code>(255, 255, 255) </code></pre> <p>To convert the image to monochrome, you just average the three values - so, the last three lines of code would become..</p> <pre><code>cpixel = pixels[x, y] bw_value = int(round(sum(cpixel) / float(len(cpixel)))) # the above could probably be bw_value = sum(cpixel)/len(cpixel) all_pixels.append(bw_value) </code></pre> <p>Or to get the luminance (weighted average):</p> <pre><code>cpixel = pixels[x, y] luma = (0.3 * cpixel[0]) + (0.59 * cpixel[1]) + (0.11 * cpixel[2]) all_pixels.append(luma) </code></pre> <p>Or pure 1-bit looking black and white:</p> <pre><code>cpixel = pixels[x, y] if round(sum(cpixel)) / float(len(cpixel)) &gt; 127: all_pixels.append(255) else: all_pixels.append(0) </code></pre> <p>There is probably methods within PIL to do such <code>RGB -&gt; BW</code> conversions quicker, but this works, and isn't particularly slow.</p> <p>If you only want to perform calculations on each row, you could skip adding all the pixels to an intermediate list.. For example, to calculate the average value of each row:</p> <pre><code>from PIL import Image i = Image.open("myfile.png") pixels = i.load() # this is not a list width, height = i.size row_averages = [] for y in range(height): cur_row_ttl = 0 for x in range(width): cur_pixel = pixels[x, y] cur_pixel_mono = sum(cur_pixel) / len(cur_pixel) cur_row_ttl += cur_pixel_mono cur_row_avg = cur_row_ttl / width row_averages.append(cur_row_avg) print "Brighest row:", print max(row_averages) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload