Spinner Icon Generation Script

A potential employer asked me to code up a quick Android application as a challenge. I enjoyed doing it, but in the course of building it I discovered, surprisingly, that “image loading” spinner art was not just a simple web search away – or, at least, I wasn’t able to find any easily. Sure, I could pay for one – but why would I do that? Instead, I quickly cranked out something by hand, so I could submit the application. However, I thought to myself that as soon as I had a moment, I should write a quick script to generate a pretty spinner (prettier than I did by hand, that’s for sure.) Here’s the script:

#! /usr/bin/python

import math
from PIL import Image

def makeSpinner(filename, size, thickness):
    """Make a spinner image. A square image of size x size will be allocated.
    The spinner will be contained in a ring with an outer diameter of size
    and an inner diameter of size-thickness."""

    image = Image.new('RGBA', (size,size), 0)
    center = int(size / 2.0)
    radius = int(size / 2.0)

    angle = 0.0
    anglestep = math.atan(0.5 / radius) * (180.0 / math.pi)
    while angle < 360.0:
        bright = int(255 * (1.0 - angle/360.0))
        color = (bright, bright, bright, 255)
        angle_radians = math.pi * angle/180.0
        for r in range(radius-thickness, radius):
            x = round(center + r * math.cos(angle_radians))
            y = round(center - r * math.sin(angle_radians))
            coordinate = (int(x), int(y))
            #print coordinate, color
            image.putpixel(coordinate, color)
        angle += anglestep

    image.save(filename + '.png', 'PNG')

# Make the spinners for the application.
makeSpinner('ic_progress', 128, 16)
makeSpinner('ic_progress_small', 64, 8)

And here are the outputs of this script. I think they look pretty nice (although the one part that doesn’t show well here is the transparency):

ic_progress_small

ic_progress

 

This is just another example of how, right out of the box, with all its libraries, Python is a fantastic and versatile tool. I particularly like the ability to so easily generate and manipulate images! The whole project, including the script shown above, can be found here, on my github page.

 

This entry was posted in Android, Programming, python, Tools. Bookmark the permalink.

Comments are closed.