View Single Post
Old 09-06-2012, 11:20 AM
farshizzo farshizzo is offline
WorldViz Team Member
Join Date: Mar 2003
Posts: 2,849
Here is a sample script that will capture the screen to a texture over a specified time interval and send a downsized version of the image to all the connected websocket clients. The image is downsized and compressed to PNG in a separate thread, so it should not slow down the main graphics thread too much, depending on the size of your window. If you need to reduce the network traffic, you could try compressing to JPG instead.
import viz
import vizhtml
import viztask
import base64
import Image
import StringIO


code = """
	<title>vizhtml Screen Share Example</title>
	<script type="text/javascript" src="vizhtml.js"></script>
<script language="javascript">

var socket = new VizardWebSocket();

socket.onevent('open', function(e){
	document.getElementById('status').innerHTML = 'connected';

socket.onevent('close', function(e){
	document.getElementById('status').innerHTML = 'waiting for connection';

socket.onevent('set_image', function(e){
	document.getElementById('screen').src = 'data:image/png;base64,' +;


<div>Status:</div><div id='status'>waiting for connection</div></br>
<img id='screen'>

# http://localhost:8080/vizhtml/screen_share

# Size of image to send
IMAGE_SIZE = [160,120]

# Time interval (in seconds) to send screen capture

def ScreenCaptureTask():

	# Blank texture that will hold screen capture
	tex = viz.addBlankTexture([1,1])

	def SendScreenImage(texture):

		# Compute downsample size while preserving original aspect ratio
		width, height = texture.getSize()[:2]
		ratio = min( IMAGE_SIZE[0] / float(width), IMAGE_SIZE[1] / float(height) )
		if ratio < 1.0:
			size = [ int(round(width * ratio)), int(round(height * ratio)) ]
			size = [ width, height ]

		# Convert texture to PIL image
		im = Image.frombuffer('RGB',[width,height],texture.getImageData(),'raw','RGB',0,1)

		# Resize image and flip
		im = im.resize(size).transpose(Image.FLIP_TOP_BOTTOM)

		# Save image to base 64 encoded PNG buffer
		output = StringIO.StringIO(),'PNG')
		buf = base64.b64encode(output.getvalue())

		# Send buffer to all connected clients

	while True:

		# Wait for capture interval
		yield viztask.waitTime(CAPTURE_INTERVAL)

		# Capture screen to texture
		yield None

		# Send texture to clients asynchronously
		yield viztask.waitDirector(SendScreenImage,tex)

viztask.schedule( ScreenCaptureTask() )
Reply With Quote