Python Pillow 如何截屏或将文字转为图片

Python Pillow How to take a screenshot of the screen or turn text into an image

美好的一天。

在我的工作中,有时我不得不使用脚本从数字或 ID 中获取一些信息。就像下面的例子:

我使用的脚本:

$sh script.sh 9999999999999 abc    

屏幕显示的信息:

number: 9999999999999

SQL*Plus: Release 1.2.3.4 Production on Wed Jan 10 12:35:22 2017

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database Enterprise Edition Release 1.2.3.4 - 32bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


NUMBER||','||SERVICE||','||CONTRACT_DATE||','||COMFIRMATION
--------------------------------------------------------------------------------
9999999999999,SERVICE_NAME_1,01-OCT-13 10.44.30 AM,Y
9999999999999,SERVICE_NAME_2,08-DEZ-14 09.52.57 AM,N
9999999999999,SERVICE_NAME_3,26-FEB-16 11.42.30 AM,Y

Disconnected from Oracle Database Enterprise Edition Release 1.2.3.4 - 32bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

我想知道如何将给我的任何脚本 returns 转换成图像,例如截取我的终端屏幕截图。

现在是转向 Python 包索引 PyPI 的好时机。它为许多事情提供了模块,包括从 Python 截取屏幕截图。

考虑,例如pyscreenshot.

使用 pip 或您的本地 platform-specific 安装程序安装,然后:

import pyscreenshot
im = pyscreenshot.grab()
im.save('ss.png')

im 是 PIL/Pillow 兼容图像,易于操作或保存到文件。