Friday, October 19, 2007

Adding a comment to a PNG or JPG header with Ruby and RMagick

Whenever you create and distribute an image, you should consider putting information in the header, such as the image title and your name and usage restrictions (if any). If you're putting the image into the public domain, you should specify that the image is a public domain image.

These issues are discussed in depth in my web page, Implementing an RDF Schema for Pathology Images.

The header of an image is a text block that is not visible when the image is viewed, but which can easily be stripped from the image.

Here is are two short Ruby scripts. The first script enters a comment into an image header. The second script extracts and displays the comment.


script pdfadd.rb
#!/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


script pdf2.rb
#!/usr/local/bin/ruby
require 'RMagick'
include Magick
walnut = ImageList.new("out.jpg")
walnut.properties{|name, value|
print "#{name} #{value}\n"}
exit




This scripts require RMagick, and instructions for acquiring RMagick are available.

The first script, pdfadd.rb, adds a comment to an image and copies the png image to
a jpg image.

The second script, pdf2.rb, extracts and prints the comment from the jpeg image.

The output of the second script is:



C:\ftp>pdf2.rb
Comment Developmental Lineage
Classification
of Neoplasms
Copyright (C) 2007
by Jules J. Berman
and distributed under
the GNU Free
Documentation License
JPEG-Colorspace 1
JPEG-Sampling-factors 1x1



The image, neo1.png, is:





In June, 2014, my book, entitled Rare Diseases and Orphan Drugs: Keys to Understanding and Treating the Common Diseases was published by Elsevier. The book builds the argument that our best chance of curing the common diseases will come from studying and curing the rare diseases.



I urge you to read more about my book. There's a generous preview of the book at the Google Books site. If you like the book, please request your librarian to purchase a copy of this book for your library or reading room.

- Jules J. Berman, Ph.D., M.D. tags: common disease, orphan disease, orphan drugs, rare disease, disease genetics