的Python:管道请求图片参数标准输入 [英] Python: piping Requests image argument to stdin

查看:351
本文介绍了的Python:管道请求图片参数标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送参数传递给子标准输入。就我而言,它是与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆