Posted: July 14th, 2010 | Author: jaymz | Filed under: code, open source | Tags: django, ooyala, python | 1 Comment »

Ooyala is a feature rich video hosting platform. Something I needed recently was some way to link the data over at ooyala into a django site I’m building. So I wrote myself a library. It’s available over at github and it’s called (predictably) django-ooyala. Currently there is a management command syncooyala to pull in all the data using the Backlot Query API. These imported items are then linked to a specific URL. Finally in your templates there is a ooyala_video tag which when given the current path (via request.path) returns the <script> tags needed for it to render.
Expect some updates as I flesh it out into the front end over the next few days.
Updated (11th August): I have added in analytics support. You can now make requests for video stat’s for a given account or video. The facebook SDK has also been added with a new template tag to output the headers in your template for a given video. Remember to request whitelisting from facebook for SWF embeds to work.
from django.http import HttpResponse
from ooyala.library import OoyalaAnalytics
from ooyala.constants import OoyalaConstants as O
from ooyala.models import OoyalaItem
def backlot_query(request):
req = OoyalaAnalytics(video=OoyalaItem.objects.all()[0].embed_code, \
method=O.ANALYTIC_METHODS.VIDEO)
ooyala_response = req.process()
return HttpResponse(ooyala_response.toprettyxml(), mimetype="text/xml")
Posted: July 14th, 2010 | Author: jaymz | Filed under: code | Tags: android, java | 2 Comments »
Update (v1.4.0) National Rail & TFL Travelcard support has been added. (August 2nd)
Update (v1.3.2) Some layout rejigging has been done, defaults added and a few more menu options for resetting/prefilling. The preferences layout has also been tidied. (July 17)
Updated (v1.2.0) I have updated this app to disable submission until all the station/date info is added. I’ve also added some more labels to show what’s been entered so far. You must make sure you’ve filled in all the preferences or else it’ll fail (Click menu>preferences). (July 16)
A few days ago I was in bed trying to sleep and I suddenly thought it would be good if there was an app to submit refunds for the tube as you come out. I had in my head the idea of a timer that would count down from 15 minutes and then alert you that you were in valid refund territory.

After I coded said timer, I pushed it live without refund submissions working. I also did a quick google to see what else there was only to discover that there was a company that released the exact same app on the iPhone about 3 months ago. Let this be a lesson that if you are about to code something as an exercise don’t go googling first or you’ll inevitably be put off because “whats the point”.
Several hours later today and it is now submitting refund applications to TFL and I get an email confirmation with the reference number. I’m very happy if incredibly tired. The app is free for now, all the others that do these on Blackberry/iOS are paid for. There doesn’t seem to be one for android yet, the guys that did the iPhone app said they where working on one way back in March or April, not quite sure what happened to it. They’re about to release the blackberry one so for now this is the only refund submission app on android
Took me basically 2 days part time.

From a technical point of view this is making use of the apache http libraries to do a GET request for the TFL refund page. It parses that to grab the ASPX Viewstate variable and session cookies. Then it grabs the data from the main activity and combines that with the data stored in shared preferences for the form variables. The POST request is processed and the response parsed again to find the reference number. That’s then displayed via a Toast call. Fairly simple, having to deal with the viewstate crap was probably the most annoying part, that and debugging the actual POST.
Posted: July 2nd, 2010 | Author: jaymz | Filed under: code | Tags: android, django, java | 1 Comment »
Quite a while back I was trying to get my head around the fairly complex ListView control for android and something I thought perfect for it was the set of radio shows over at spine magazine.
I have had this code sitting around for a while and I thought why not knock out the news code and release it and then just continue to work on it and update it. As the saying goes, release early, release often! I think it’ll be easy enough to add in reviews using the same sort of code to get the data and just re-using the show layout. This particular app was a lot more complicated than the django-reference thing, so here is some of what I’ve learned… You can get this in the marketplace now. Bug reports are expected & indeed welcome.

Warning! Technical crap follows.
It takes a bit of time to get your head around it, essentially you subclass the ArrayAdapter class with a container class to hold your list view elements. The ArrayAdapter is a templated class so you first need another class that is actually going to hold your data. This is were you’ll need to create a class for your data and then instantiate the ArrayList with this type. The code to hold my news post data is very simple:
package eu.jaymz.spinemag;
import android.util.Log;
public class NewsPost {
private String title;
private String description;
private String date;
private String author;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Then in my main activity class I cast the templated ArrayList to a NewsPost class:
ArrayList<NewsPost> spr_news = new ArrayList<NewsPost>();
With the data structure sorted I need a method in my ListView activity to actually fill in the ArrayList with data. This is very typically something along the lines of the following, here I read the JSON data from a URL, decode it and assign it to the class members, finally appending it to the ArrayList from earlier.
private void getPosts() {
try {
URL url = new URL("url for data");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
spr_news = new ArrayList<NewsPost>();
JSONObject json = new JSONObject(str);
JSONArray posts = json.getJSONArray("posts");
for(int x=0; x < posts.length(); x++) {
JSONObject post_data = new JSONObject(posts.get(x).toString());
NewsPost post = new NewsPost();
post.setTitle(post_data.get("title").toString());
post.setDescription(post_data.get("description").toString());
post.setDate(post_data.get("date").toString());
post.setAuthor(post_data.get("author").toString());
spr_news.add(post);
//Log.d("NEW", "added a post - "+post.getTitle());
}
} catch (Exception e) {
}
runOnUiThread(setPosts);
}
With all the above in place the code is at the point where there is now an array full of structured data for our ArrayAdapter to show. To do this the ArrayAdapter needs subclassed to work with our earlier NewsPost classes members:
private class NewsAdapter extends ArrayAdapter<NewsPost> {
private ArrayList<NewsPost> items;
public NewsAdapter(Context context, int textViewResourceId, ArrayList<NewsPost> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.post, null);
}
NewsPost o = items.get(position);
if (o != null) {
/* hmm, this looks proper ugly */
TextView title = (TextView)v.findViewById(R.id.post_title);
TextView description = (TextView)v.findViewById(R.id.post_description);
TextView date = (TextView)v.findViewById(R.id.post_date);
TextView author = (TextView)v.findViewById(R.id.post_author);
description.setText(o.getDescription());
title.setText(o.getTitle());
author.setText(o.getAuthor());
date.setText(o.getDate());
}
return v;
}
}
All this code is really doing is loading the layout resource XML for our row and binding the data from the array elements members to views within the XML. Finally we need to tell the list view to update. Typically the above code has all been kicked off and ran in a thread, populating the ListView when it’s actually finished without stalling the phone itself.
for(int i=0;i<spr_news.size();i++) {
n_adapter.add(spr_news.get(i));
}
m_ProgressDialog.dismiss();
n_adapter.notifyDataSetChanged();
All the above is doing is updating the ArrayAdapter and adding our ArrayList elements to display. We then signal that the data has changed which will cause the ListView itself to update. Simple!
Again, I must make the point of how verbose this all feels when I’m coding in python all day. I’m sure it needs a good refactor to now but I’m a lot more familiar with the way android works with layouts and indeed strong typing
Posted: June 30th, 2010 | Author: jaymz | Filed under: code, open source | Tags: blogger, django, python | 3 Comments »
I’ve been working a lot on Vans recently and they have a large number of blogs that are currently hosted on the blogger platform. This works really well for them, they have a straightforward & easy to use blog platform that does what they need it to do. The sites themselves work great as they are but the integration into the main site isn’t quite as nice.
If you go to the blogs section on vans as it stands it will pull in an XML feed of all the current blogs and display them on one giagantic page. It can take a fair bit of time to load and its hard to see how each different blog gets displayed as its really one big list. For now it’s not so easy to pull in bits of content from the various blogs without someone having to mess with files or copy & paste, with the build in django I wanted to create a much cleaner & easier way to work with this content.

For this I introduce django-blogger, a django application which will integrate Google Blogger blogs via their RSS feeds. As it is it will import the blogs for a given profile id and then sync up with the latest data via their RSS feeds. These can be enabled easily for your given blogs. When you first install it comes with some admin actions which will all ow you to sync up the blogs manually all at once. There is also a management command, syncblogs, which is more suited to scheduling an update via cron (if you’re going to use cron you might be interested in django-crontab).

This works on the feeds and not an archive, so it doesn’t require authentication, just access to the feed URL. A basic template is included to show how to render out the blog posts & blogs as a menu, I override these myself for the format I need. The app itself now lets me pull content from any of the blogs and use it within the rest of the django based site cleanly & easily. Rather than directly reading and displaying via the feed URL I’m creating actual objects for each post and blog so it’s easily extendible also, say to return posts in various formats or pulling images from each blog post to create blog galleries automatically.

The code is available from GitHub.
Posted: June 15th, 2010 | Author: jaymz | Filed under: code, graphics, open source, processing | Tags: java | No Comments »
I very much like to play with
processing.org when I’ve got some downtime. I’ve wrote a small particle engine a while back in the framework and this time I thought I’d try and do something a little more arty from the outset. My inspiration for this “sketch” was the many splatter paintings by Jackson Pollock. I can remember going to see some of his work at the Tate modern in London quite a long time ago. I had always thought it a bit poor as “art” when I was a kid but later on I developed an appreciation for the format. If you have no idea what I’m talking about, this is an example:

Now my sketches don’t look much like this but I did capture some of that splattering chaotic effect that I wanted. The basic process is very simple, a class called a Walker will randomly make its way across the canvas. As it moves the vector distance of each step is calculated. If the step is big enough it causes a burst of circles to be drawn, each with a slightly random position & varied alpha to give it some texture. To avoid it becoming a little too busy I’ve limited it to a set palette at runtime which can be regenerated as walkers are added.

I was quite happy with the random scatter effect as different pools of colour made their way through the canvas space. I thought it would be a nice addition to add in some basic boundary conditions so I added a quick polygon class and added in an array of those. A few functions later and I could click around the screen defining new polygons. Using the well known Jordan Curve theorem we can tell if the walker is currently within an arbitrary polygon (without holes of course). Here’s an example video of it in action. Heart’s seem pretty easy to draw compared to anything crazy complex and give a good idea of the effect:
I’ve added quite a few options to the code now allowing me to switch on & off the boundaries as well as add an arbitrary number of polygons. If the vertices overlap then the way the “point in polygon” algorithm works it will flag each “contained” area as a solid.
Finally here is a nicely captured heart shape. It gives a nice idea of the kind of effect when combined with a boundary. I think playing around with the way the walkers are coded could be fun, for example doing a neighbour check and tweaking the randomised movement accordingly could create a weak flocking style system with Brownian motion driving it.
If you’re interested in checking the code out you can
find it over on github. It should run for you ok out of the box within your processing environment.
Posted: May 28th, 2010 | Author: jaymz | Filed under: android, code | Tags: android, apps, django, java | No Comments »
Very late last night, as I was enjoying “The Design & Evolution of C++” (an excellent book, even if you’re not a C++ dev), I took a short break and was browsing around on the android market place and noticed there was no django app. I’ve recently picked up an HTC desire and have been really enjoying developing on it. I’m currently working on an application for Crooked Tongues to allow people to post & comment their sneakers (don’t ask…) and I thought this would be a quick app to do and get out there. Anyone starting out developing apps will be aware that one of the hardest things is just thinking of something that isn’t already crowded out by what’s already available.
Basically, all I wanted was a more dedicated browser for the django docs. The content there is excellent so I quickly knocked together an application that lets you jump to major sections, set a page zoom by default and switch version easily. All in all it only took a few hours of development and thanks to the approval procedure it’s already available to install.

It’s a glorified web view but it’s a start point and its at least out there now. I’m getting a lot more used to the dev process now on android. The total code for this app weighs in around 400 lines – around 80 of those being XML to define the layout and strings used. The main activity class then manipulates the XML defined webview component to react to the (again XML) menu. My main constructor method looks like this:
@Override
public void onCreate(Bundle savedInstanceState) {
Resources res = getResources();
version = res.getString(R.string.default_version);
base_url = res.getString(R.string.url_base);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new DjangoWebClient());
final View zc = webview.getZoomControls();
FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
mContentView.addView(zc, ZOOM_PARAMS);
zc.setVisibility(View.GONE);
setZoom();
loadDocs();
}
The remainder of the methods are basically just reacting to the menu choices and directing the webview url appropriately. All in all quite simple.

Posted: May 18th, 2010 | Author: jaymz | Filed under: code, open source | Tags: chrome, google, javascript | No Comments »
I have been coding in java for about a week now (when I’m not coding in Python of course) and I fancied a break away from it. I’m not entirely sure why but I decided to take a look at coding chrome extensions. These are in fact very, very easy to make. The tube status thing below took me just over an hour to get how I wanted. If you’re running chrome you can get the installer here. I think I’ll do some more work on it to add customization – that was my initial motivation to do this, the first one I installed from the current extension gallery just showed the little panel of status’ from the site and I wanted to just pick a few lines & tweak the output.

In the end it’s easier to just code my own. It makes use of the excellent REST api provided by Ben Dodson. Creating such an extension is trivial. You can make your life a bit easier if you just include jquery from the google ajax api’s. Then its really just a collection of a few html files (in this case just the one popup.html). All my HTML is doing is making an AJAX request for the JSON data from Ben’s api. That comes back and it simply renders out a few div’s with the colors set and based on the status a unicode entity to add some fun to it.
The method of making extensions on chrome really is a breeze and the documentation is plentiful, so hats off to google yet again for making me happy to be an open source, linux loving developer. The extension is now in the chrome gallery and available here.
Posted: May 16th, 2010 | Author: jaymz | Filed under: android, code | Tags: android, django, java | 2 Comments »
I recently picked up an HTC desire and have been loving android as a platform. I’m not entirely sure why I didn’t go for android to begin with (apart from doing my friend Spangsberg a favour by buying out his iPhone contract before he left the country). I dabbled a little with objective C but not owning a full on mac for dev’ing put me off spending a significant time at home coding for the device. That and it seemed like a lot of work to get going (probably because I use Linux almost all day so am not used to the Mac toolchain).
Working with android on linux has been a total pleasure. Mostly. I’m still getting re-used to the verbosity of java compared to my day-to-day python but the experiance has been a good one so far. I’ve basically been learning by looking at the sample apps supplied combined with a lot of googling and reading of android dev forums. There is a real wealth of documentation out there and there’s an absolute ton of general java code to solve most problems. This is where I’m at about a week into it (spending a couple of hours every few days on it).

The HTTP posts are working now as is uploading a camera shot, you can store user/passwords and then it’ll do the form posts with that data. For now I have it just “logging” in to a dummy setup on my own server – that side is just some PHP for now. The real backend runs python (django), for now its just easier to run that data through some PHP on my blog server. The data for the views comes via a JSON array, that data is served up via python and is essentially just a REST interface to the TIA area. I’m mainly concentrating on just learning my way around the SDK but already I’m seeing that theres a lot of power in combining the android platform with django (and maybe django-pistons on top) to create rather powerful connected mobile apps.
Posted: May 3rd, 2010 | Author: jaymz | Filed under: code, graphics, open source | Tags: bash, css, imagemagick | No Comments »
I’ve been rebuilding the Crooked Tongues forum and one of the things we’ve been mindful of is trying to stick to some good practises with regards optimzing the site for fast loading. One of the easy ways to decrease your page load times is to use CSS sprites. This is just stiching together all your common images into one big one and then using background positions & width/heights in CSS to offset and “crop” the big image. This way you only get the one HTTP request made to request it.

If you’ve got hundreds of small images it can really speed up your page. A prime candidate for sprites are typically your buttons, header images and in the case of a forum, emoticons. Crooked uses lots of custom designed icons and when writing them out as individual images for the post toolbar really slowed down the page. There was no chance I was going to do it manually so I turned to a stable of my web development toolset – Imagemagicks convert.
Convert can do a lot. I’ll show you two such ways it made my life easier and cut down on the time I had to spend doing grunt work. First, here’s how the toolbar looks:

I wrote some bash to basically loop over a folder of images and then using convert to pull the width & height info, I use that to write the CSS file. Finally I loop over the images once more using the append command to output one big, long image. (gist)
#!/bin/bash
# uses imagemagick to stich together all images in a folder and
# then writes a css file with the correct offsets along with a
# test html page for verification that its all good
if [ $# -gt 0 ]
then
if [ $3 ]
then
ext="."$3; # the extension to iterate over for input files
else
ext=".gif"; # the extension to iterate over for input files
fi
name=$1; # output will be placed in a folder named this
if [ $2 ]
then
classname=$2"-sprite";
else
classname=$1"-sprite";
fi
css="$name/$classname.css";
html="$name/test.html";
rm -fr $name;
mkdir $name;
touch $css $html;
echo "Generating sprite file...";
convert *$ext -append $name/$classname$ext;
echo "Sprite complete! - Creating css & test output...";
echo -e "<html>\n<head>\n\t<link rel=\"stylesheet\" href=\"`basename $css`\" />\n</head>\n<body>\n\t<h1>Sprite test page</h1>\n" >> $html
echo -e ".$classname {\n\tbackground:url('$classname$ext') no-repeat top left; display:inline-block;\n}" >> $css;
counter=0;
offset=0;
for file in *$ext
do
width=`identify -format "%[fx:w]" "$file"`;
height=`identify -format "%[fx:h]" "$file"`;
idname=`basename "$file" $ext`;
clean=${idname// /-}
echo ".$classname#$clean {" >> $css;
echo -e "\tbackground-position:0 -${offset}px;" >> $css;
echo -e "\twidth: ${width}px;" >> $css;
echo -e "\theight: ${height}px;\n}" >> $css;
echo -e "<a href=\"#\" class=\"$classname\" id=\"$clean\"></a>\n" >> $html;
let offset+=$height;
let counter+=1;
echo -e "\t#$counter done";
done
echo -e "<h2>Full sprite:</h2>\n<img src=\"$classname$ext\" />" >> $html;
echo -e "</body>\n</html>" >> $html;
echo -e "\nComplete! - $counter sprites created, css written & test page output. ~jaymz";
else
echo -e "There should be at least 1 argument!\n\tbuildSprite.sh output_folder classname input_extension"
fi
The script outputs the names incrementing by one, I’ve gone over myself and turned them into “nice” names. All in all its taken maybe 10 minutes to convert it all into a sprite and nice CSS file. If you were using a primary key field like 1, 2, 3… etc then you could probably fudge it to not even have to name the id’s “nicely”.

Imagemagicks convert can also do some modification of your files. It can for example convert your image to a black & white copy. It’s not a great deal of work to then loop over a set of images, create b&w versions in a temporary folder and then stitch the two versions together to get a color→b/w sprite for hovers. I’ve actually seen people do this manually and spend an age at it, with this method you output them once then sit back for the 30 seconds or so whilst the computer does the work.
for file in *.jpg; do convert -colorspace Gray $file bw/$file; done
for file in *.jpg; do convert $file bw/$file -append sprites/$file; done

It’s times like this that I am reminded of just how much time you can save with a little shell script and the right command line tools. Not everthing needs point & click.
Posted: April 22nd, 2010 | Author: jaymz | Filed under: code, graphics, open source | Tags: color, pil, python | 1 Comment »
Recently we’ve been working rather hard on a new look and complete re-launch of the underlying code for the whole of crookedtongues.com We’re a python shop these days so its running on django with a host of apps and the best part of over 100,000 lines of code.
One of the things that I was tasked with was the data migration and import and I’ll write about that post launch, what I fancy getting out now is how I’ve worked on adding color codes automagically to the store’s entire product database. More or less, its a little rough round the edges but gives fairly good results on a lot of our products. First off you can find the source code in a raw (read that as ‘I’m still working on it personally so take it as it comes’) way over at github. As a taste of what it does, here’s a couple of existing product shots:
It works quite well for colors that fall into the center areas of the color wheel. For my analysis I’ve segmented it by 30° so there are 12 bands of color. I’ve found that yellow & light greens are the most tricky to pull out accurately from an instinctive perception view of the result. By that I mean what looks very much yellow can get labelled as a bright orange instead as values close to the segment boundary (45°) cause an imbalance towards the orange side.

The basic process is working well for products with one dominating color, for multi-color shots it can pull out the relative levels of each tones hue band but blindly chooses the largest, however slight and uses that for its naming output. A better way would be to do some further analysis on the collected results for each image – one way to enforce some confidence that the main hue really is the main color would be to compare its standard deviation from the average across the sample set. A small σ would indicate a more uniform mix of colors – as we have already sampled out low saturation points this would indicate an image of strong multicolor. Taking the set of hue/tones within the 90th percentile and then comparing their relative deviations of count would allow identifying (and quantifying) the number of colors – so it would be possible to say mainly dark blue with a little light red.

If two bands had similar counts it is also more likley that the color in question is not two separate closely aligned colors but in fact a single color at the midpoint (or thereabouts) of them. A scheme to compare these before the final decision would no doubt improve the detection of yellow which seems to constantly tip into the orange side of the wheel. All in all working on this has been a decidedly pleasant break from data imports, javascript and enough MVC style code to do me a lifetime.
Since I wrote the above I’ve tweaked it somewhat to pull out gray (and black/white) counts also. It works out the rough percentage that corresponds to and if its over a certain limit for black or white will output a value for that also (with a lot of the product shots they’re on a white background so I need to be more granular with that than just regular colors).
#!/usr/bin/python
# -*- coding: utf-8 -*-
# A color analysis script to help you label your store's products with color data
# automagically. It will take either a single file or scour an entire folder for
# folders of images and do each one individually printing a summary of what it
# thinks is the correct color value. A work in progress...
#
# ~jaymz | @jaymzcampbell | jaymz.eu
#
#
# MIT Licesnsed for what its worth, copy: http://www.opensource.org/licenses/mit-license.php
import Image
import ImageFilter
import os
import glob
import sys
import colorsys
import re
from copy import copy
from operator import itemgetter
from decimal import Decimal
output = open('colors.csv', 'w')
# Pixels will be first compared to these values before being
# added to the data list of color information on the first pass
LBOUND = 0
UBOUND = 255
MIN_SATURATION = 30 # avoid washed out pixels influencing counts
# Base folder for the processFolder function, it'll iterate over here on subfolders
FOLDER = '/home/jaymz/documents/crooked-docs/data-export/store-migration/product-images/'
# Meh, i need to flip between these two, you can probably tweak this
SUMMARY_FORMAT, SQL_FORMAT = True, True
# Names based off: http://bluelobsterart.com/wordpress/wp-content/uploads/2009/03/rgb-color-wheel-lg.jpg
COLOR = ['RED', 'ORANGE', 'YELLOW',
'LIME', 'GREEN', 'TURQUOISE',
'CYAN', 'OCEAN', 'BLUE',
'VIOLET', 'MAGENTA', 'RASPBERRY',
]
TONE = ['DARK', '', 'BRIGHT']
# via the createColorSQL.py file , addition added in GRAY/BLACK/WHITE to after this
SQL_IDS = {'DARK YELLOW': 7, 'DARK ORANGE': 4, 'BRIGHT GREEN': 15, 'BRIGHT ORANGE': 6, 'DARK RED': 1, 'BRIGHT OCEAN': 24, 'BRIGHT RED': 3, 'DARK OCEAN': 22, 'YELLOW': 8, 'OCEAN': 23, 'BRIGHT YELLOW': 9, 'RASPBERRY': 35, 'GREEN': 14, 'BRIGHT TURQUOISE': 18, 'CYAN': 20, 'MAGENTA': 32, 'RED': 2, 'ORANGE': 5, 'BLUE': 26, 'TURQUOISE': 17, 'LIME': 11, 'BRIGHT LIME': 12, 'DARK MAGENTA': 31, 'DARK LIME': 10, 'BRIGHT MAGENTA': 33, 'BRIGHT VIOLET': 30, 'DARK VIOLET': 28, 'DARK BLUE': 25, 'BRIGHT BLUE': 27, 'VIOLET': 29, 'BRIGHT RASPBERRY': 36, 'DARK TURQUOISE': 16, 'DARK CYAN': 19, 'BRIGHT CYAN': 21, 'DARK GREEN': 13, 'DARK RASPBERRY': 34}
pcnt = 0
def trimFloat(val, places=2):
return float(repr(val)[0:places+2])
def withinBounds(allowance, _rgb):
rgb = copy(_rgb)
diff = 0
allowance = Decimal(repr(allowance))
for c in rgb:
for d in rgb:
dec_d = Decimal(repr(d)).quantize(allowance)
dec_c = Decimal(repr(c)).quantize(allowance)
diff = abs(dec_d-dec_c)
if (d != c) and diff>allowance:
return False
return True
def processImage(i, name=None):
""" Scales down the image, blurs it to ease the blending of the color values
and reduce spikes from anomolies. It then samples pixels creating a list of
colors. This list is then looped over to build counts which are placed into
bins of 30° hue's seperated into three based on their value. Pixels less than
a certain saturation are discarded. """
global pcnt
i = i.resize((200,200))
i = i.convert("RGB")
i = i.filter(ImageFilter.BLUR)
d = i.getdata()
cnt = 0
h = [] #holds the hsv info
grays = [] #holds just gray content
black_count = 0
white_count = 0
total_samples = 0
for p in d:
cnt = cnt + 1
if cnt == 8: #take every 4th pixel
if p[0]>LBOUND and p[1]>LBOUND and p[2]>LBOUND and p[0]<UBOUND and p[1]<UBOUND and p[2]<UBOUND:
r = trimFloat(float(p[0])/255)
g = trimFloat(float(p[1])/255)
b = trimFloat(float(p[2])/255)
if not withinBounds(0.02, (r,g,b)):
h.append(colorsys.rgb_to_hsv(r,g,b))
else:
if (r+g+b)/3>0.94:
white_count += 1
elif (r+g+b)/3<0.3:
black_count += 1
else:
grays.append(colorsys.rgb_to_hsv(r,g,b))
total_samples += 1
cnt = 0 #reset sample counter
h.sort()
grays.sort()
bin_width = 30 # size of hue slices (degress)
max_bin = 360
darks = [0] * int(max_bin/bin_width)
mids = [0] * int(max_bin/bin_width)
lites = [0] * int(max_bin/bin_width)
for p in h[::]:
hue = p[0]*360
sat = p[1]*100
val = p[2]*100
if sat >= MIN_SATURATION:
bin_number = ((int(hue)+15)/bin_width)%(max_bin/bin_width)
if val<33:
darks[bin_number] += 1
elif val>33 and val < 66:
mids[bin_number] += 1
else:
lites[bin_number] += 1
#print "HUE BIN: %s VALUE : %d" % (int(hue)/bin_width, int(hue))
c = 0
data = zip(darks, mids, lites)
if SUMMARY_FORMAT:
for x in data:
print '%d %s : %s %d°' % (c, COLOR[c], x, c*bin_width)
c += 1
# the following area needs a rework. the index technique works alright as long
# as counts and values dont all match up, then it starts picking the first one
# so this needs re-writing to better order the list data
darks_sort, mids_sort, lites_sort = darks[::], mids[::], lites[::]
darks_sort.sort()
mids_sort.sort()
lites_sort.sort()
sorted_counts = (darks_sort, mids_sort, lites_sort)
primary_idx = (darks.index(sorted_counts[0][-1]), mids.index(sorted_counts[1][-1]), lites.index(sorted_counts[2][-1]))
primary_cnts = (darks[primary_idx[0]], mids[primary_idx[1]], lites[primary_idx[2]])
tone = primary_cnts.index(max(primary_cnts))
max_hbin = primary_idx[tone]
pcnt += 1
if SUMMARY_FORMAT:
print "\nDominant Hue: %s %s" % (TONE[tone], COLOR[max_hbin])
if SQL_FORMAT and name and max(primary_cnts) > 30:
output.write('%d, %s, %s\n' % (pcnt, name, SQL_IDS[' '.join([TONE[tone], COLOR[max_hbin]]).strip()]))
sorted_counts[0][-1], sorted_counts[1][-1], sorted_counts[2][-1] = (0, 0, 0) # kind of reset the primary to null
for l in sorted_counts:
l.sort()
primary_idx = (darks.index(sorted_counts[0][-1]), mids.index(sorted_counts[1][-1]), lites.index(sorted_counts[2][-1]))
primary_cnts = (darks[primary_idx[0]], mids[primary_idx[1]], lites[primary_idx[2]])
tone = primary_cnts.index(max(primary_cnts))
max_hbin = primary_idx[tone]
if SUMMARY_FORMAT:
print "Secondary Hue: %s %s" % (TONE[tone], COLOR[max_hbin])
if SQL_FORMAT and name and max(primary_cnts) > 30:
pcnt += 1
output.write('%d, %s, %s\n' % (pcnt, name, SQL_IDS[' '.join([TONE[tone], COLOR[max_hbin]]).strip()]))
# area to rewrite ends...
gray_total = [(g[0]+g[1]+g[2])/3 for g in grays]
gray_average = reduce(lambda x,y : x+y, gray_total)/len(gray_total)
black_percent = black_count/float(total_samples)*100
gray_percent = len(gray_total)/float(total_samples)*100
white_percent = white_count/float(total_samples)*100
if SUMMARY_FORMAT:
print "\nAverage Gray: %s (samples: %0.1f%%), White count: %s (%0.1f%%), Black count: %s (%0.1f%%)" % (gray_average, gray_percent, white_count, white_percent, black_count, black_percent)
print "Total samples taken: %s\n\n" % total_samples
if SQL_FORMAT:
if black_percent > 10:
pcnt += 1
output.write('%d, %s, %d\n' % (pcnt, name, 38))
if gray_percent > 10:
pcnt += 1
output.write('%d, %s, %d\n' % (pcnt, name, 37))
if white_percent > 30:
pcnt += 1
output.write('%d, %s, %d\n' % (pcnt, name, 39))
# Helper functions follow along with __main__ def
def processFolder(folder):
for image_folder in glob.glob(folder+'*'):
try:
folder_images = []
for image in os.listdir(image_folder):
if "jpg" in image and "._" not in image:
folder_images.append(image)
folder_images.sort()
j = os.path.join(image_folder, folder_images[1])
if SUMMARY_FORMAT:
print "working: "+j
i = Image.open(j)
processImage(i, image_folder.split('/')[-1])
except:
pass
def processFile(_file):
i = Image.open(_file)
processImage(i)
if __name__ == "__main__":
try:
if 'product-images' not in sys.argv[1]:
processFile('product-images/'+sys.argv[1])
else:
processFile(sys.argv[1])
except IndexError:
processFolder(FOLDER)
output.close()