Saturday, October 20, 2007

More Ruby image header and image format tricks

This blog is just an elaboration on an earlier blog. Again, it uses RMagick and ImageMagick. Details for using these within Ruby have been posted.

Here's a script, pdfadd.rb, that takes a png image file and creates a jpg image file with a text comment.



#!/usr/local/bin/ruby
require 'RMagick'
include Magick
walnut = ImageList.new("neo1.png")
walnut.cur_image[:Comment] =
" Developmental Lineage
Classification
of Neoplasms
Copyright (C) 2007
by Jules J. Berman
and distributed under
the GNU Free
Documentation License"
walnut_copy = ImageList.new
walnut_copy = walnut.cur_image.copy
walnut_copy.write("out.jpg")
exit



Once the comment has been put into the jpg file, we can extract it with another Ruby script, pdf2.rb.



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



The output looks like this:



c:\ftp>ruby pdf2.rb
Developmental Lineage
Classification
of Neoplasms
Copyright (C) 2007
by Jules J. Berman
and distributed under
the GNU Free
Documentation License



We can also directly transform one image type into another. Here, we will transform a png image into a pdf image.



#!/usr/local/bin/ruby
require 'RMagick'
include Magick
walnut = ImageList.new("neo1.png")
walnut_copy = ImageList.new
walnut_copy = walnut.cur_image.copy
walnut_copy.write("out.pdf")
exit



As a caveat, when you transform an image between different formats, your comment text may be lost if the receiving format doesn't recognize the attribute object. So you can transfer Comment text from png to jpg, but you can't directly transfer the comment over to pdf.

- 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.