Jan
09

Detecting Bag Colors

This feature was put together by modifying some handy code I was able to find on the web, including:

Extracting the most common colors was easy enough and the php package above meant that I didn't have to code it.  All you do is open the image and look at it pixel by pixel to figure out the color.  The code I implemented from phpclasses.org made this trivial but then I ended up with a list of hex color code and how many times that color was used in the image.  Now I needed to turn those hex codes into English that is meaningful to users and that is where the code from github came in.  That code is a phpized version of a nice javascript that you can see here only it wasn't quite done.  I made a few changes to stick the hex color codes and associated names into a DB table.

So now I had all of the pieces that I needed, a class to pull out the colors and the code to take those colors and find the closest matching name.  I'm only using the top three colors and then tossing out any duplicates that arrise when the hex codes are converted to text and then displaying this on the product pages and stuffing it into the solr index.  This work is done whenever a product page is loaded for the first time and then simply pulled out of the database.

It works fairly well and it mostly returns the colors you would expect.  For example,

this image ended up being tagged with:  Cinnabar (Red) , Coral Red, Radical Red.   The DB of names has the hex color code, the color name and then a sort of hue or color family.  If that family is included in the name of the color, i.e. Coral Red, I don't bother including it in the color list and if it's not there, I do as in Cinnabar (Red).  You can see the 1,600 or so different colors.

The thousands of products are still being processed so the index is not finished yet, but it is working quite well.  It's nice that a search on something like "shiraz" returns exactly what I was hopping for it to return.

I'm sticking the colors, along with the image name and node ID into a table rather than using CCK fields attached to the product nodes which is probably the "right" way to do it, but it is working for me.  The work is being done in the template's page.tpl.php file if the page being loaded is a product node.  If a value is in the DB, it uses it, if not, it figures out the colors to use, stick them into the DB and uses them for the meta keywords and displaying to the user.  When indexing the content, solr just queries that table based upon the node id to get the text colors to use.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Contact Bag Idiot | About Bag Idiot | Site Map | Privacy Policy | Terms and Conditions

Copyright © 2010 - 2012 Bagidiot.com

Site Development and Hosting by Crash-Land, LLC