的Python:管道请求图片参数标准输入 [英] Python: piping Requests image argument to stdin
问题描述
我试图发送参数传递给子标准输入。就我而言,它是与Requsts下载图像。
I am trying to send arguments to a subprocess' stdin. In my case, it is an image downloaded with Requsts.
下面是我的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
不幸的是,子什么都不做,我也得到任何错误。有什么不对我的code,而且是有跨平台的解决方案?
Unfortunately, the subprocess does nothing, and I get no errors. What is wrong with my code, and is there a cross-platform solution?
推荐答案
ICAT
查询终端,看看有什么尺寸调整图像大小,但管道不适合作为一个终端,你最终空的输出。从 ICAT
帮助信息状态:
大的图像自动调整到终端的宽度,除非-k选项。
Big images are automatically resized to your terminal width, unless with the -k option.
当您使用 -k
开关的输出。
有没有必要在这里流,你可以离开装载到要求
,并通过在响应主体,非德codeD:
There is no need to stream here, you can just leave the loading to requests
and pass in the response body, un-decoded:
img = requests.get(url)
proc = subprocess.Popen(['icat', '-k', '-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
stdout, stderr = proc.communicate(img.content)
的标准错误
值将是空的,但标准输出
应该包含变换的图像数据(ANSI颜色转义):
The stderr
value will be empty, but stdout
should contain transformed image data (ANSI colour escapes):
>>> 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'
这篇关于的Python:管道请求图片参数标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!