如何使用 Python Image Library 将 libvirt 流转换为不同的图像类型?
How do I use Python Image Library to convert a libvirt stream to a different image type?
按照下面的示例代码,完美运行:
import sys
import libvirt
domName = 'TestAppliance'
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
dom = None
try:
dom = conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
stream = conn.newStream()
imageType = domain.screenshot(stream,0)
file = "Screenshot of " + dom.name()
fileHandler = open(file, ’wb’)
streamBytes = stream.recv(262120)
while streamBytes != b’’:
fileHandler.write(streamBytes)
streamBytes = stream.recv(262120)
fileHandler.close()
print(’Screenshot saved as type: ’ + imageType)
stream.finish()
conn.close()
exit(0)
(来自 https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/ch12s02.html)
但是,这会生成一个 .PPM 图像,大小约为 3.5mb。
如何使用 python 流将 PPM 图像推送到 PIL 中,以便 PIL 可以将其转换为(更小的).PNG(或其他格式)?
我知道我可以保存它并转换它,但这似乎...倒退了。
更多的研究给了我答案。打开连接并找到您的 VM 后,使用它截取屏幕截图,并将其保存为 PNG:
vstream = conn.newStream()
imageType = dom.screenshot(vstream, 0)
file = dom.UUIDString() + '.png'
bstream = vstream.recv(8192)
pyStream = BytesIO()
while bstream != b'':
pyStream.write(bstream)
bstream = vstream.recv(8192)
pyStream.seek(0)
image = Image.open(pyStream)
image.save(file, 'PNG')
pyStream.close()
print('PNG saved from '+str(imageType))
vstream.finish()
大小差异为 9.1k (PNG) 与 3.2mb (PPM),因此物有所值!
按照下面的示例代码,完美运行:
import sys
import libvirt
domName = 'TestAppliance'
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
dom = None
try:
dom = conn.lookupByName(domName)
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
stream = conn.newStream()
imageType = domain.screenshot(stream,0)
file = "Screenshot of " + dom.name()
fileHandler = open(file, ’wb’)
streamBytes = stream.recv(262120)
while streamBytes != b’’:
fileHandler.write(streamBytes)
streamBytes = stream.recv(262120)
fileHandler.close()
print(’Screenshot saved as type: ’ + imageType)
stream.finish()
conn.close()
exit(0)
(来自 https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/ch12s02.html)
但是,这会生成一个 .PPM 图像,大小约为 3.5mb。
如何使用 python 流将 PPM 图像推送到 PIL 中,以便 PIL 可以将其转换为(更小的).PNG(或其他格式)?
我知道我可以保存它并转换它,但这似乎...倒退了。
更多的研究给了我答案。打开连接并找到您的 VM 后,使用它截取屏幕截图,并将其保存为 PNG:
vstream = conn.newStream()
imageType = dom.screenshot(vstream, 0)
file = dom.UUIDString() + '.png'
bstream = vstream.recv(8192)
pyStream = BytesIO()
while bstream != b'':
pyStream.write(bstream)
bstream = vstream.recv(8192)
pyStream.seek(0)
image = Image.open(pyStream)
image.save(file, 'PNG')
pyStream.close()
print('PNG saved from '+str(imageType))
vstream.finish()
大小差异为 9.1k (PNG) 与 3.2mb (PPM),因此物有所值!