title: How do subprocess.Popen pipes work in Python?
body: <p>I am fully aware of the existence of related questions, but all of them are very fuzzy and fail to clearly explain what is going on every step of the way. The examples provided are often not tested and provide no information on how to adapt them to different scenarios. Here are the questions as well as Python's documentation:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/295459/how-do-i-use-subprocess-popen-to-connect-multiple-processes-by-pipes">How do I use subprocess.Popen to connect multiple processes by pipes?</a></li>
  <li><a href="https://stackoverflow.com/questions/7389662/link-several-popen-commands-with-pipes">link several Popen commands with pipes</a></li>
  <li><a href="https://docs.python.org/2/library/subprocess.html#popen-objects" rel="nofollow noreferrer">https://docs.python.org/2/library/subprocess.html#popen-objects</a></li>
  </ul>
  
  <p>This is essentially what I am trying to achieve, but in Python:</p>
  
  <pre><code>curl http://asdf.com/89asdf.gif | convert -resize 80x80 - - | icat -k -
  </code></pre>
  
  <p>Here is what I have after hours for frankensteining together bits and parts of the aforementioned answers:</p>
  
  <pre><code>import requests
  import subprocess
  from subprocess import Popen, PIPE, STDOUT
  url = 'http://asdf.com/89asdf.gif'
  img = requests.get(url)
  p1 = subprocess.Popen(['convert', '-resize', '80x80', '-', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  p2 = subprocess.Popen(['icat', '-k', '-'], stdin=p1.stdout, stdout=subprocess.PIPE)
  p1.communicate(img.content)
  print p2.stdout
  </code></pre>
  
  <p>Here is my code again, this time improved based on your answers:</p>
  
  <pre><code>import requests
  from subprocess import Popen, PIPE, STDOUT
  url = 'http://asdf.com/89asdf.gif'
  img = requests.get(url)
  p1 = Popen(['convert', '-resize', '80x80', '-', '-'], stdin=PIPE, stdout=PIPE)
  p2 = Popen(['icat', '-k', '-'], stdin=p1.stdout, stdout=PIPE)
  p1.stdin.write(img.content)
  p1.stdin.close()
  p1.stdout.close()
  output = p2.communicate()[0]
  print output
  </code></pre>
  
  <p>Note: <a href="https://github.com/atextor/icat" rel="nofollow noreferrer"><code>icat</code></a> outputs images in 256-color capable terminals. I already managed to print its output successfully in <a href="https://stackoverflow.com/questions/26980469/python-piping-requests-image-argument-to-stdin">another question</a>. A typical image looks like this in a terminal once processed by <code>icat</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/Drg35.png" alt="icat doing what it does best"></p>
  
  <p>Please correct me where I am wrong, but this is my current understanding:</p>
  
  <ul>
  <li><code>p1.communicate(img.content)</code>: This sets <code>p1</code>'s STDIN.</li>
  <li><code>p1</code>'s STDIN is <code>subprocess.PIPE</code>, which is what <code>p1.communicate(mg.content)</code> provides.</li>
  <li><code>p2</code>'s STDIN is <code>p1</code>'s STDOUT, which is the image resized by <code>convert</code>.</li>
  <li>I then <code>print</code> the <code>p2</code>'s STDOUT, which should be the image with ASCII colors provided by <code>icat</code>.</li>
  </ul>
  
  <p>Questions:</p>
  
  <ol>
  <li>I have seen both <code>stdin=subprocess.PIPE</code> and <code>stdin=PIPE</code>. What is the difference?</li>
  <li>Why is the first STDIN provided after the other subprocesses are defined?</li>
  </ol>
  
  <p>Could someone explain what is actually going on in the process and what is wrong with my code? This whole piping business must be simpler than it seems, and I'd love to understand how it works.</p>