Tuesday, February 19, 2008

Python script adds an image description to a jpeg header

In September, I published a web document, with Bill Moore, that explained how images can be annotated with textual information that describes the image. The full document is distributed under a GNU license and is available at:

http://www.julesberman.info/spec2img.htm

Digital images that do not convey descriptions of their binary image have very little scientific value. The document has methods, in Perl and Ruby, for inserting textual information into image headers.

In today's blog, I show how textual information can be inserted and extracted from a jpeg image, using the Python programming language.

Chris Stromberger has prepared two public domain Python scripts in October, 2004:

http://www.fetidcascade.com/public/minimal_exif_writer.py


and

http://www.fetidcascade.com/public/minimal_exif_reader.py


These scripts use the exif header space, a popular method used by many manufactureres of digital cameras, to put textual information into the headers of digital images in popular formats, particularly jpeg.

http://www.exif.org/

Here is my script, exif.py, that uses both of Chris Stromberger's scripts to insert a description into a jpeg header and then to extract and display the text.

#!/usr/bin/python
import datetime
from minimal_exif_writer import MinimalExifWriter
file = open("hamlet.txt", "r")
text = "\nImage annotation date: "
text = text + str(datetime.date.today())
text = text + "\nImage description:\n"
text = text + file.read()
file.close()
f = MinimalExifWriter('trial.jpg')
f.removeExif()
f.newImageDescription(text)
f.newCopyright('Dr. Sympatico', addYear = 1)
f.process()
from minimal_exif_reader import MinimalExifReader
g = MinimalExifReader('trial.jpg')
print g.imageDescription()
print g.copyright()
print g.dateTimeOriginal()
exit

In this example, I built a string variable holding an image description composed of the contents of a small file (hamlet.txt) containing a few lines of Hamlet's soliloquy. Here is the output of exif.py

c:\>python exif.py
Image annotation date: 2008-02-19
Image description:
To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished. To die, to sleep--
To sleep--perchance to dream: ay,
there's the rub,

2008 Dr. Sympatico

Please back up your original images before using them with this script. Some digital camera manufacturers use a proprietary (non-standard) header that can be corrupted when the exif.py script tries to add data to an expected exif partition.

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, genetic disease, image descriptors, image header, image specification, python, exif, python scripts, image annotation, metadata