Python: piping Requesting an image argument for stdin

I am trying to send arguments to subprocess' stdin. In my case, this is an image uploaded with Requsts.

Here is my code:

from subprocess import Popen, PIPE, STDOUT
img = requests.get(url, stream=True)
i = img.raw.read()
proc = subprocess.Popen(['icat', '-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
proc.communicate(i)
#proc.stdin.write(i) # I tried this too

      

Unfortunately the subprocess does nothing and I don't get any errors. What's wrong with my code and is there a cross platform solution?

0


source to share


1 answer


icat

asks your terminal to see what dimensions resize the image, but the pipe doesn't fit as a terminal and you end up with an empty output. Background from icat

reads:

Large images automatically resize to the width of your terminal, unless with the -k option.

When you use the output -k

, the output is displayed.

There is no need to pass information here, you can just leave the load up requests

and pass the response body, not decoded:



img = requests.get(url)
proc = subprocess.Popen(['icat', '-k', '-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
stdout, stderr = proc.communicate(img.content)

      

The value stderr

will be empty, but stdout

must contain the transformed image data (ANSI color screens):

>>> import requests
>>> import subprocess
>>> url = 'https://www.gravatar.com/avatar/24780fb6df85a943c7aea0402c843737'
>>> img = requests.get(url)
>>> from subprocess import PIPE, STDOUT
>>> proc = subprocess.Popen(['icat', '-k', '-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
>>> stdout, stderr = proc.communicate(img.content)
>>> len(stdout)
77239
>>> stdout[:20]
'\x1b[38;5;15m\x1b[48;5;15m'

      

+1


source







All Articles