Posts tagged with “twitcolors”

September 12

Generating an Image's Color Scheme Using Imagick

UPDATE:It seems that the TinyMCE environment I used to edit this post stripped the <pre> I used to wrap the code. Everything should be fine now though!

When I got the idea behing TwitColors the first question I asked myself was "What's the best way to get the colors out of an image?". And since it was the main component for the whole application and it analyzed about 60 full-size images it also had to be fast, since this was only the beginning of the whole analytic process that is behind TwitColors.

This being said I almost instantly eliminated (or `hated` to be more exact) the idea of iterating through all the image's pixels and finding out their color and then using a math formula to "merge" the colors. It could be done but it would take too much time that the app doesn't have. After forgeting about this approach I remembered seeing an interesting function in the Imagick library. That function is Imagick::quantizeImage(). And it does exactly what I neede it to do: it "cuts" the number of colors inside an image to the number you give it. And I wanted 10.

Without any more hustle I'm going to present you with the code I used to extract 10 unique colors from an image:

$im=new Imagick($image);
$im->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
$im->uniqueImageColors();
$it=$im->getPixelIterator();
foreach ($it as $row=>$pixels)
	foreach ($pixels as $column=>$pixel)
		{
		$rgba=$pixel->getColor();
		}

Basically everything it does is to load the image as a new Imagick object, reduce the number of colors in the image to 10 RGB colors (using the Imagick::quantizeImage() function I was talking about earlier), delete all unrequired pixels from the image keeping only one pixel for any unique color (that is what Imagick::uniqueImageColors() does). Up until this point the full-size image has been converted to a tiny image that is 10px wide and 1px high. Now everything that remains to be done is to get the colors of those pixels in a PHP readable format - that is either and HEX value or an RGB array. This is also done with Imagick by creating a new PixelIterator object using the Imagick::getPixelIterator() function and then using a simple loop to go through the matrix of pixels and getting their color using the ImagickPixel::getColor() function that returns an RGBA (red, green, blue, alpha) array of the color.

That's it. Now you can easily get the color scheme of any image without needing any specialized software and straight from your PHP scripts.

03:09 PM | 0 Comments | Tags: , , , , , ,
August 23

TwitColors.com

UPDATE: Great news for designers! TwitColors now allows you to download a certain user's color scheme in .ACO format. You can find the download link below the user's color listing.

A few days ago I made a blog post stating that I'm working on a bunch of projects that I really hope to get online asap. Well, I have a good news for both you and me. One of them is online.

The project I'm talking about now is TwitColors, a new way to explore Twitter from a colorful perspective.

It basically analyzes the Twitter profile image of a given person and brakes it to up to 10 unique colors which are then matched to a database of thousands of already discovered colors and Twitter users and generates it's color scheme as well as tops for the most and least used colors in profile images among different tweeps.

Example color schemes: http://twitcolors.com/encu, http://twitcolors.com/loic

 

But enough with the talk. Go and check it out at http://twitcolors.com

 

04:33 PM | 0 Comments | Tags: , , ,