[Image-SIG] PNG text chunk patch

Andrew M. Kuchling akuchlin@cnri.reston.va.us
Fri, 7 May 1999 13:01:10 -0400 (EDT)


Here's the simple patch required to write out the contents of the info 
dictionary as text chunks:

*** PngOrig	Fri May  7 12:53:42 1999
--- PngImagePlugin.py	Fri May  7 12:54:40 1999
***************
*** 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)])

As a small bonus, here's a completely untested implementation of
chunk_zTXt, to read in compressed text chunks.  

*** PngOrig	Fri May  7 12:53:42 1999
--- PngImagePlugin.py	Fri May  7 12:54:40 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
+ 
  
  # --------------------------------------------------------------------


-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
A successful tool is one that was used to do something undreamed of by its
author.
    -- S.C. Johnson