, , , , , , , , , , , , ,

Wouldn’t it be cool if we could navigate through space using a bit of Python code? Turns out that it’s dead easy. Which is just as well, as I promised my scandalous chum Arkwood that I would find a star and name it after him. He wants immortality, you see. Don’t we all?

Anyway, I’ve already written some Python that navigates through Google Street View, taking screenshots each step of the way. My post Green Cities using Python shows what a random stroll down the streets of Google can uncover. So why not take the same approach with space, and randomly whizz about the universe taking pictures of what we see?

Let’s use Microsoft’s Worldwide Telescope to navigate space. First up, I create a web page that will display somewhere in the universe:

<!DOCTYPE html >
<meta charset="utf-8" http-equiv="X-UA-Compatible" content="chrome=1, IE=edge"/>
<title>Space Explorer</title>
<script src="http://www.worldwidetelescope.org/scripts/wwtsdk.aspx"></script>
// Worldwide Telescope object
var wwt;
// initialize wwt and fire wwtReady event
function initialize() {
	wwt = wwtlib.WWTControl.initControl("WWTCanvas");

// custom code for WWT
function wwtReady() {

    // wwt object settings

    // go to position specified in url querystring

// get value from url querystring parameter
function getUrlParam(key) {
    return unescape(window.location.search.replace(
        new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"

<body onload="initialize()">
<div id="WorldWideTelescopeControlHost">
	<div id="WWTCanvas" style="width:1500px; height:750px;" />

The WorldWide Telescope SDK has all the detail of how to go about building these web pages.

I have hosted the web page on my Windows 7 Home Premium PC, using IIS (Internet Information Services).

Let’s take a brief tour of the web page HTML and JavaScript…

The function wwtReady is where I get to add my custom code, once the Worldwide Telescope object has been initialized. I turn off some display features (such as the crosshair symbol ‘+’) and then navigate to a certain position in the universe.

But where in the universe shall we go? Well, we set our location in the web page’s url querystring:


As you can see, I have a getUrlParam function, which can collect these querystring values and use them to whizz to a certain spot in the night sky. Thanks to Christofer Eliasson for providing the function via stackoverflow.

Terrific. Now all we need is some Python code to make use of our web page:

import webbrowser
from time import sleep
import ImageGrab
import numpy
import cv2
from datetime import datetime

# constants
SPACE_EXPLORER_URL = "http://localhost/space_explorer.htm?"
SAGITTARIUS_PARAMS = "ra=286.485&dec=-27.5231666666667&fov=60&instant=false"
AQUARIUS_PARAMS = "ra=334.345&dec=-9.21083333333333&fov=60&instant=false"

# navigate space
def navigate(params):
    # navigate space using parameters
    webbrowser.open(SPACE_EXPLORER_URL + params)

    # take screenshot of space
    screenshot = ImageGrab.grab(bbox=(0,110,1520,870))
    img = cv2.cvtColor(numpy.array(screenshot), cv2.COLOR_RGB2BGR)

    # save screenshot to disk
    cv2.imwrite('space/images/{}.jpg'.format(datetime.now().strftime('%Y%m%d_%Hh%Mm%Ss%f')), img)

# loop through space forever!
while True:

    # go to Sagittarius

    # go to Aquarius

The function navigate opens our web page in a browser, supplying the querystring parameters for a certain position in the universe.

Once the program has paused for 30 seconds (enough time for the browser to render our image of space) we take a screenshot and save it to disk. We use ImageGrab to take the screenshot, and then let OpenCV convert it to a desired format and save it.

Now, the plan is to use OpenCV to analyse these images of space, using its computer vision techniques to detect planets, stars and the like. I’ve already made a start with my post OpenCV Morphological Transformations in Space. But for now we’ll simply save the image to disk.

That’s it! The program just keeps looping between Sagittarius and Aquarius. Though, just as I have done with Google Street View, the code can be easily modified to take a random jaunt through the universe, snapping pictures as we go.

Here’s a screenshot of Sagittarius rendered in my browser:


And here’s Aquarius:


Arkwood was impressed. He put down his copy of The Inscrutable Diaries Of Rodger Saltwash and gave me a hug.

‘Top-notch!’ he exclaimed, ‘Once you have found a new star, and named it after me, I will be an immortal God Of The Universe!’

‘What about me?’ I asked.

‘You will be a thimble of dust and bones and nothingness. Forgotten forever.’

He really knows how to spur me on.


The Python code was run on my Windows 7 PC using the free and excellent Python Tools for Visual Studio.