Playing with images: Rmagick, Ruby on Rails, large coffee

|
After a few minutes of messing around I found it was actually rather simple to perform basic manipulation of images with Rmagick and Ruby. At work, we'd been looking for a simple way of allowing folk to upload an image, pop it into a BLOB in postgres and generate a small thumbnail. So here's a brief rundown of what I did (btw: i'm assuming your modifying an existing model to store the image):

Install Rmagick
(i'm currently running Vista (not for long) and this is the best for windows as you'll struggle to find win32 binaries for the other ruby image mungers). Have a look here for the latest Rmagick.

Prepare your Database.
Do a migration to allow you to store some meta-data and the actual image data. I did something like this:


class AddImageData < ActiveRecord::Migration
def self.up
add_column :foo, :image_content_type, :string
add_column :foo, :image_filename, :string
add_column :foo, :image_binary_data, :binary
add_column :foo, :image_thumb_binary_data, :binary
end

def self.down
remove_column :foo, :image_content_type
remove_column :foo, :image_filename
remove_column :foo, :image_binary_data
remove_column :foo, :image_thumb_binary_data, :binary
end
end


Run a rake db:migrate to prepare your database.

Write some tests to validate your model class is actually going to do something with the image data. I wrote a couple of simple tests to prove our accessor method below "uploaded_picture=" gets hit when i create a new "Foo", and that the image_data fields are read into the model correctly.

Fix up, Look sharp.
Now fix up your model to do something with the new image. I did this (not much code for what you get!):


def uploaded_picture= image_data
if image_data
img_orig = Magick::Image.from_blob image_data.read
thumb = img_orig.first.scale(200, 200)

self.image_filename = base_part_of(image_data.original_filename)
self.image_content_type = image_data.content_type.chomp
self.image_binary_data = img_orig.first.to_blob
self.image_thumb_binary_data = thumb.to_blob
end
end

def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/,'')
end


The code has eyes
Now modify your view to accept a file upload.
Note that you need to set :html => {:multipart => true} for the tag or you're in for a long and fruitless evening.

Add this to your form where "f" is the form in scope.

<%= f.file_field :uploaded_picture, :label => "Image" %>


And you're done. Hope this saves you some time! PS - sorry about the screwy CSS. I'm using a crufty wordpress theme. Will fix when i have some time.

Why we don't care...and Pepper Theory

|
Samaritan

One of my first posts on this blog described the tendency of humans to behave in a manner which is ultimately self-serving. We consider the interests of ourselves over others. I called this selfishness, with a note that one should disregard the negative connotations of the word.

Months later, browsing the excellent TED talk archives I have come across Daniel Goleman's talk "Why aren't we all good samaritans"; a promenant psychologist, essentially supporting the theory (albeit indirectly) that we do principally, act to serve ourselves or our emotional need. Even, or rather especially in 'selfless' acts such as giving to charity; we get an emotional 'hit' by doing so. Yannis made some interesting points also supported in this talk. Check it here.

Pan fried duck breast in red wine jus

|
My friend and colleague Tim posted a good recipe for warm spicy winter apple juice so i thought i'd post one of my faves. Its actually a French recipe so there are many other permutations of this available.

So, you need 2 medium/large duck breasts (skin on)
  • 1 Medium orange

  • 1/2 (350ml approx) red wine (Medoc or Malbec grape wines are good but whatever you've got)

  • 2 Cloves garlic

  • Half Kilo Baby new potatoes

  • 150ml fresh double cream

  • 15g Fresh chives

  • Sprig of Thyme

  • 10g Cornflour

  • 50g butter


  • Take the duck breasts and score the fatty side with a sharp knife in a sort of lattice pattern. Grate the zest of the orange into a bowl, then add the orange juice. Finely chop the garlic and add to the orange bowl, along with the red wine and the thyme sprig. Place the duck breasts into the bowl so the meaty side is submerged. If you've got time, leave this to marinade for 3 hours or so, if not, leave it as long as you can.

    Boil some water and add your potatoes. Depending on their size, they will need approx 10-12 minutes for smallish (4cm diameter) spuds. Once the potatoes are on, heat a frying pan on a high heat, with a very small amount of oil in. When the pan is hot, place the duck breasts in the pan fatty side down to sear the surface. After a minute or two the fatty side will have a lovely golden brown colour. You may wish to pour some of the fat out here if there's a lot (save it for making roasties!). Turn the breasts and sear the meaty side for a minute too. This seals the meat and keeps the juices in. Turn the heat down lower now, and pour in your marinade (wine & orange mix). Allow this to reduce on a low heat (such that the liquid is not boiling). Once the liquid has reduced down, the duck and potatoes will be ready.

    Drain the potatoes and place back in the pan with the butter on a medium/high heat. Add seasoning to taste here. Fry the potatoes in the butter, rolling them round the pan so they don't burn.

    Take the duck breasts from the reduction jus and place on warmed plates, or in the oven on gas mark 1. Mix the cornflour with a small amount of cold water and mix. Add this to the reduction jus along with the cream.

    Place duck and potatoes on plates adding the jus as desired. Garnish with Chives.

    Fin.

    DRM and Downey clones

    |
    No DRM

    My feed-reader picked up an interesting snippet of info the other day. Sony BMG, (Sony and BMG combined music arm) are to drop DRM protection on all music distributed from their back catalogue. Fantastic news then for music lovers everywhere! As is so often the case, now a major label has annouced this bold step (and yes it is bold for the music industry), others will surely follow suit.

    Does this mean that Sony have rethought their strategy with regard to online sales, or is it merely acceptance of the inevitable? Either way, I admire the thought behind this, and the lead they have taken, especially given sony's dreadful record in bygone days for hounding down all those "evil pirates" when in fact, it was the DRM software causing the problem.

    Anyway, let bygones be bygones. I can't help but think there's some character like PSD jumping for joy somewhere inside Sony's corporate wall, in fact i'm sure of it.