用Python在linux终端上显示图像

2024-06-17 02:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用w3mimgdisplay在图像终端上显示图像,并且正在查看Ranger文件管理器的源代码。我正在查看的文件可以找到here。在

用这个,我做了一个简单的程序。在

import curses
from subprocess import Popen, PIPE

process = Popen("/usr/libexec/w3m/w3mimgdisplay",
                stdin=PIPE, stdout=PIPE, universal_newlines=True)

process.stdin.write("echo -e '0;1;100;100;400;320;;;;;picture.jpg\n4;\n3;'")
process.stdin.flush()
process.stdout.readline()
process.kill()

每次我进来

^{pr2}$

在终端中,它正确地打印图像,但是运行python脚本没有任何作用。如何将程序的输出写入终端?在


Tags: 文件图像import程序终端管理器here源代码
1条回答
网友
1楼 · 发布于 2024-06-17 02:26:34

shell echo命令将新行添加到其输出的末尾(除非使用-n开关,否则您需要在命令末尾添加一个新行来模拟这一点。在

另外,您应该编写字符串内容,而不是echo命令本身,因为它直接发送到w3mimgdisplay进程,而不是shell。在

我也不确定为什么readline。我建议改为使用.communicate()命令,因为它可以确保不会陷入罕见但可能的读/写缓冲区竞争状态。或者,最好的方法是直接使用更简单的^{}

import subprocess
subprocess.run(["/usr/libexec/w3m/w3mimgdisplay"], 
    input=b'0;1;100;100;400;320;;;;;picture.jpg\n4;\n3;\n')

相关问题 更多 >