title: How do I use Python Image Library to convert a libvirt stream to a different image type?
body: <p>Following the sample code below, works perfectly:</p>
  <pre><code>​import sys
  ​import libvirt
  ​domName = 'TestAppliance'
  ​conn = None
  ​try:
  ​    conn = libvirt.open(&quot;qemu:///system&quot;)
  ​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 = &quot;Screenshot of &quot; + 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)
  </code></pre>
  <p>(from <a href="https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/ch12s02.html" rel="nofollow noreferrer">https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/ch12s02.html</a>)</p>
  <p>However, this produces a .PPM image, which is around 3.5mb in size.</p>
  <p>How can I use a python stream to push that PPM image in PIL, so PIL can convert it to a (smaller) .PNG (or other format)?</p>
  <p>I know that I can save it, and convert it, but that seems so... backwards.</p>