Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that the problem here is that the source file you are using is compressed (probably a .jpeg file). When you pass <code>sys.argv[1]</code> to <code>file.save</code> PIL decides whether to compress the "polarized" image based on the file extension. So if <code>sys.argv[1]=="test.jpg"</code> then the file written to disk will be compressed. </p> <p>Compression can induce colours in between the pure red and black that you are expecting.</p> <p>An easy way around this problem is to write the output to file using an uncompressed format such as .png instead.</p> <p>Incidentally, this "polarization" is usually referred to as "thresholding" and the two-tone image you are generating is a thresholded image.</p> <p>Also, I don't think it's a great idea to use the word <code>file</code> as a variable name, since it is used for other things, e.g. <a href="http://docs.python.org/2/library/functions.html#file" rel="nofollow">http://docs.python.org/2/library/functions.html#file</a></p> <p>The version of your code below demonstrates the difference between letting PIL write a compressed version of your thresholded image, compared to an uncompressed version:</p> <pre><code>import sys from PIL import Image im=Image.open("test.jpg") im=im.convert('RGB') w,h=im.size color={} for i in range(w): for j in range(h): aux=im.getpixel((i,j)) if aux &gt;(200,200,200): im.putpixel((i,j),(255,0,0)) else: im.putpixel((i,j),(0,0,0)) im.save("test_copy.jpg") im.save("test_copy.png") im=Image.open("test_copy.jpg") im=im.convert('RGB') w,h=im.size color={} for i in range(w): for j in range(h): aux=im.getpixel((i,j)) if aux not in color: color[aux]=1 else: color[aux]+=1 print "Compressed file stat" print color im=Image.open("test_copy.png") im=im.convert('RGB') w,h=im.size color={} for i in range(w): for j in range(h): aux=im.getpixel((i,j)) if aux not in color: color[aux]=1 else: color[aux]+=1 print "Uncompressed file stat" print color </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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