NIGHT SKY GENERATION

This is just a small program I wrote in Python to generate a starry night sky. It uses Pillow for image processing, and it was a fun exercise in basic random image generation. I wanted to make a program that you could run over and over and it could always spit out a pretty picture that was completely unique each time. Naturally, the program employs a lot of random number generation using NumPy as well as some basic linear math for galaxies. The largest challenge is trying to get the stars to look somewhat natural in color and shape, and that's where I think the program needs most development. For now however, I think the results are acceptable.

I came up with the idea during development of this website. I needed a pretty night sky backround for my homepage, but I didn't want to just use some stock image and I don't have any graphic design experience, so I just wrote a script for it in Python. I wanted it to look like a night sky if it was unobstructed by the atmosphere and light pollution; where the milky way and all the stars are highly visible. I also really wanted to have a program that could use random number generation to build a really pretty picture every time without the need for human tweaking, so you could just run the code and get a unique and beautiful picture.

The first thing I did was get Pillow (an image-processing library for Python) as well as NumPy (a math library) for its many random number generators, and I started drawing little yellow circles at random locations with random sizes. This made a picture, but not really anything close ot a night sky yet. The real night sky has stars distributed in clusters broken by empy space. It also has a big streak of stars and gasses across the night sky where the Milky Way passes through our sky. Finally, stars are not just yellow, they're usually closer to blue or white with variation based off of temperature. I shelved the coloring issure and went on to create a galaxy system. I wrote a function which takes an x-value, a slope, and an intercept, and spits out a y value corresponding to the linear equation defined by those parameters. This line will be the center of the galaxy which the stars will cluster around.

To actually get the stars clustered around the line, I used a normal distribution random number generator to randomize the distance of each star from the center line, with an average distance to make the distribution appear more natural. This produces a nice line distribution , but doing this only once leaves the 'galaxy' looking kind of weak. So I draw stars around the line multiple times with different normal distributions to produce a layered sort of look, which I think makes it look much more natural. Finally, I just kept my initial code to generate stars at random locations to build a background behind the galaxy.

Next it was time to tackle the coloration. Initially I just tried generating each star with a random RGB value and tried restricting the range of the R, G, or B in order to keep the stars within the range of reality (no green or purple stars, etc). This didn't really work very well. I realized that stars' colors are determined by their temperature and there is a direct correlation between a star's color and temperature in kelvin. So after doing a little research, I found a Python script that does just this! It converts degrees kelvin into an RGB value. All I had to do was give each star a random temperature based on the range of actual star temperatures, and then I could get rid of all impossible RGB values for stars. This works well, although there are still too many red stars which are generally pretty uncommon in the night sky. I think it actually makes the image look more interesting from an aesthetic perspective, even if it's not quite accurate.

The last thing I've done was add star clusters, in addition to the galaxy system. This one is pretty simple. I just pick a random set of points and generate stars at a random distance. It leaves the image looking a lot more realistic and beautiful.

That is as far as the script goes for now. I have several issues with it I want to improve in the future. One glaring issue is that Python is very slow, and generating high-resolution images can take several seconds. I'd like to rewrite the program in a more memory-efficient langauge like C++ to reduce processing time. Another issure is that the actual individual stars are pretty rudimentary. They're just circles with a blur effect. I'd like to introduce some kind of flare to them to make them more interesting. I would also like to include other types of astronomical bodies like comets, nebulae, planets, etc. I think the program as it exists is still compelling and I am happy with the images it produces. That's all!