I’ve had the week relatively free between jobs to relax and work on some personal projects. There are a number of things I’ve been meaning to try out and play around with.

First off, I opened up Sinatra and decided to tackle a Facebook app. I’d been wanting to try some image manipulation with profile photos so decided to tackle a simple application involving compositing photos from many smaller photos. I downloaded the base Facebook app that comes with Facebook Heroku now and was suprised at how much Facebook app development has progressed. So much is set up out of the box for you now, and the layers of code to get at different Facebook features is so thin now. It’s so much better than when we were all learning this stuff. Mogli is the library used and it provides the basic thin Facebook layer I like and seems to be fairly smartly done.

I google’d ruby image manipulation to see what the some of the latest, greatest libraries were and was surprised to see RMagick using ImageMagick was like the first hit. Tried and true, I guess. Not one to argue, I downloaded and used that since it’s familiar territory. The trick was to get the grayscale value of a piece of one image and then figure out how to have a smaller image match it in average value. It’s not too hard with ImageMagick. One line of code that I thought was quite cool here was:

value = img.resize(1,1).pixel_color(1,1).intensity/65535.to_f

This returns the average brightness of an image in one line. The intensity method is an ImageMagick shortcut that assigns different weights to the R, G, B parts of the color to determine the objective “brightness” of a color. From there, you just have to look at whether your target pixel is darker or lighter than the source image and by how much. Then I just put a black or white background depending on which way I needed to adjust and used CSS opacity in the desired ratio to get the correct overall value.

The result is here.



blog comments powered by Disqus

Published

10 March 2012

Tags