[Image-SIG] Patch for text chunks in PNGs

Andrew M. Kuchling akuchlin@mems-exchange.org
Thu, 2 Dec 1999 16:45:17 -0500 (EST)


Here's a patch of mine from a while back that didn't make it into PIL
1.0; I just want it to get into a future release.  It adds support for
reading zTXt (compressed text) chunks and writes out the contents of
the .info dictionary as text chunks.

Being able to add metadata to an image is of great importance to me;
is there a list of which image plugins actually read in image metadata
and write it out?  (Where the image format supports it, of course.)

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
I am a sociologist, God help me.
    -- John O'Neill



*** PngImagePlugin.py	Mon Aug  2 18:39:56 1999
--- /tmp/PngImagePlugin.py	Thu Dec  2 11:45:55 1999
***************
*** 198,201 ****
--- 198,213 ----
  	return s
  
+     def chunk_zTXt(self, pos, len):
+         # compressed text
+         s = self.fp.read(len)
+         [k, v] = string.split(s, "\0")
+         comp_method = ord( v[0] ) ; v = v[1:]
+         if comp_method != 0:
+             raise SyntaxError, "Unknown compression method %i in zTXt chunk" % comp_method
+         # Deflate/inflate compression was used
+         import zlib
+         self.im_info[k] = zlib.decompress( v )
+         return s
+ 
  
  # --------------------------------------------------------------------
***************
*** 408,411 ****
--- 420,429 ----
  	chunk(fp, "gAMA", o32(int(gamma * 100000.0)))
  
+     # Write text chunks
+     for key, value in im.info.items():
+         # Skip PIL-internal keys
+         if key in ['interlace', 'gamma']: continue
+         chunk(fp, 'tEXt', key + chr(0) + value)
+         
      ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])