Entropy Status Update
by Lance Laughlin
This blog post is a reference to a team project I’m working on for Advanced FOSS: https://github.com/ExplosiveHippo/Entropy
So the first week of development has officially ended! Over the next week we’ll need to finish up the rest of our functionality and start preparing for testing and packaging.
So far we have a command line tool that is successfully reading in an image and spits out some basic info about it; including, the screen resolution and format. This week I worked on and completed the functionality to allow the user to analyze an image that is hosted on the web and accessible by a URL. I was actually quite surprised by how easy it was to get this functionality working. We are already using Pillow to pull some data about the image; therefore, all I really needed to handle was actually pulling in the image from a URL and the code we have already worked flawlessly on it.
Here is a quick code rundown of what I needed to do:
import urllib import cStringIO
First off, we need to import urllib and cStringIO. urllib allows us easily open up an url and pull data from whatever it serves us. cStringIO is a helper class that allows us to use strings a bit better…I’m not 100% of all the functionality but it’s definitely helpful in this situation.
In our initializer function is where most of the magic happens.
def __init__(self, source): #Pass in the argument image location self.source = source if 'http' in self.source: print 'reading from url' file = cStringIO.StringIO(urllib.urlopen(self.source).read()) self.image = Image.open(file) else: try: self.image = Image.open(self.source) except IOError: print "Cannot load image. Be sure to include http:// if loading from a website" sys.exit()
Here we are basically checking if the value the user passed in is a web url. There may be a better way of doing this than checking if the string has http but it works for now. If it is a url we can simply load that image into our program with a single line and then instantiate it as an Image object.
We later go on to print some data about the image using the Image method, which is a part of the Pillow library:
def output(self): #Probably only print this in verbose mode in the future print "We're processing the image: " + self.source print "This " + self.image.mode + " image is in the " + self.image.format + " format" print self.image.size
We hopefully will finish off the rest of the required functionality this week. More to come!