Monday, October 22, 2007

Adding and extracting comments to a Tiff image, with Ruby

This blog is part of a series describing methods to annotate images. The purposes of image annotation is to convey important (and often necessary) textual information inside the header of your image.

The basic utilities for image display/manipulation in Ruby are posted.

For this blog, we will use the .tif version of a public domain image of submucosa selected from Gray's Anatomy and downloaded from the Wikipedia Commons.



The Ruby script tifadd.rb adds a simple comment, "Hello world" to the
tif file submu_bw.tif.


#!/usr/local/bin/ruby
require 'RMagick'
include Magick
tissue = ImageList.new("submu_bw.tif")
tissue.cur_image[:Comment] = "Hello world"
tissue_copy = ImageList.new
tissue_copy = tissue.cur_image.copy
tissue_copy.write("submu_2.tif")
exit


The Ruby script is invoked from the command line.


c:\>ruby tifadd.rb


That's all there is to it. The comment can be extracted from the new .tif file, submu_2.tif, with the Ruby script tifout.rb.


#!/usr/local/bin/ruby
require 'RMagick'
include Magick
tissue = ImageList.new("submu_2.tif")
print tissue.properties['Comment']
exit


Here is the output.


c:\ftp>ruby tifout.rb
Hello world


It's very simple, but this technique allows you to start annotating your .tif files with text.

- Jules Berman


Science is not a collection of facts. Science is what facts teach us; what we can learn about our universe, and ourselves, by deductive thinking. From observations of the night sky, made without the aid of telescopes, we can deduce that the universe is expanding, that the universe is not infinitely old, and why black holes exist. Without resorting to experimentation or mathematical analysis, we can deduce that gravity is a curvature in space-time, that the particles that compose light have no mass, that there is a theoretical limit to the number of different elements in the universe, and that the earth is billions of years old. Likewise, simple observations on animals tell us much about the migration of continents, the evolutionary relationships among classes of animals, why the nuclei of cells contain our genetic material, why certain animals are long-lived, why the gestation period of humans is 9 months, and why some diseases are rare and other diseases are common. In “Armchair Science”, the reader is confronted with 129 scientific mysteries, in cosmology, particle physics, chemistry, biology, and medicine. Beginning with simple observations, step-by-step analyses guide the reader toward solutions that are sometimes startling, and always entertaining. “Armchair Science” is written for general readers who are curious about science, and who want to sharpen their deductive skills.