Commit 63c4ce39 by Guba Sándor

vmdriver: modified screenshot to return image as byte fixes#57

How it works:
Obtain ppm byte array from libvirt
Convert it to png byte array and returns
parent f708f27e
...@@ -2,3 +2,4 @@ celery==3.0.23 ...@@ -2,3 +2,4 @@ celery==3.0.23
decorator==3.4.0 decorator==3.4.0
lxml==3.2.3 lxml==3.2.3
psutil==1.1.3 psutil==1.1.3
Pillow==2.3.0
...@@ -481,19 +481,18 @@ def send_key(name, key_code): ...@@ -481,19 +481,18 @@ def send_key(name, key_code):
def _stream_handler(stream, buf, opaque): def _stream_handler(stream, buf, opaque):
fd = opaque opaque.write(buf)
os.write(fd, buf)
@celery.task @celery.task
@req_connection @req_connection
@wrap_libvirtError @wrap_libvirtError
def screenshot(name, path): def screenshot(name):
"""Save screenshot of virtual machine. """Save screenshot of virtual machine.
Returns a ByteIO object that contains the screenshot in png format.
Image is saved to the path as name-screenshot.ppm
""" """
from io import BytesIO
from PIL import Image
# Import linuxkeys to get defines # Import linuxkeys to get defines
import linuxkeys import linuxkeys
# Connection need for the stream object # Connection need for the stream object
...@@ -506,14 +505,20 @@ def screenshot(name, path): ...@@ -506,14 +505,20 @@ def screenshot(name, path):
# Take screenshot accessible by stream (return mimetype) # Take screenshot accessible by stream (return mimetype)
domain.screenshot(stream, 0, 0) domain.screenshot(stream, 0, 0)
# Get file to save data (send on AMQP?) # Get file to save data (send on AMQP?)
fd = BytesIO()
try: try:
fd = os.open(path + "/" + name + "-screenshot.ppm",
os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0o644)
# Save data with handler # Save data with handler
stream.recvAll(_stream_handler, fd) stream.recvAll(_stream_handler, fd)
finally: finally:
stream.finish() stream.finish()
os.close(fd) #Convert ppm to png
#Seek to the beginning of the stream
fd.seek(0)
# Get the image
image = BytesIO()
ppm = Image.open(fd)
ppm.save(image, format='PNG')
return image
@celery.task @celery.task
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment