Monthly Archives: June 2013

Separating a foreground object from its background using GIMP’s Color to Alpha feature

Recently I was trying to separate a foreground image from its background so I could put the image on a new background of a completely different color.  There is a GIMP tutorial, but it failed to help me remove the ugly white or colored edges from the foreground.  Here is the image I started with:

Using that above mentioned GIMP tutorial, I tried my best to isolate only the text and place it on my background.  The best I could do was this:

The Solution

After using the Fuzzy Select tool to isolate the foreground image from it’s background (in this case, a white background) and pasting it into a new layer, I then set the entire background layer use the dark red color I wanted.  Then I used the “Color to Alpha” feature from the “Colors” menu.  It brings up this dialog:

From here, you specify the color you want made into alpha.  I left it at the default, white.  According to the GIMP documentation, this tool, “…will attempt to preserve anti-aliasing information by using a partially intelligent routine that replaces weak color information with weak alpha information. In this way, areas that contain an element of the selected color will maintain a blended appearance with their surrounding pixels”.  That is exactly what I needed.

Color to Alpha Applied

I had one final thing I wanted to do to the image, which was change the black color to a different one.  For this, I used the “Colorize” tool on the top layer, also in the “Colors” menu.

The Result

     And the zoomed out, final version:    

Return JSONP with CakePHP

Here’s a nice way to turn on JSONP for CakePHP. JSONP, or JSON with padding, is necessary when you want to include JSON from a site different than the one you’re on.

A good explanation for what JSONP is can be found here:

Just include this code on any CakePHP controller that you will be using with JSONP (or you could put it in the AppController to make it work everywhere in your app but that may add a slight performance hit). It checks to make sure that JSON is being returned before trying to apply the padding. This code overrides the afterFilter() which is the last controller action CakePHP does after doing rendering.

    // This funciton makes so JSONP works
    public function afterFilter() {

      if (empty($this->request->query['callback']) || $this->response->type() != 'application/json') {

      // jsonp response
      App::uses('Sanitize', 'Utility');
      $callbackFuncName = Sanitize::clean($this->request->query['callback']);
      $out = $this->response->body();
      $out = sprintf("%s(%s)", $callbackFuncName, $out);