Exctract GIF comment from image
Guilherme Polo
ggpolo at gmail.com
Tue Mar 11 13:39:40 EDT 2008
2008/3/11, wingi at gmx.com <wingi at gmx.com>:
> Hi,
>
> simple question: The PIL does not support reading the optional
> description in GIF Images.
>
> http://www.pythonware.com/library/pil/handbook/format-gif.htm
>
> After some reasearch I could not find a python solution for this, any
> suggestions?
>
> Thanx, Wingi.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
I did a quick thing here, try it and check if it solves the problem for you:
import os
import sys
import struct
def extract_comments(giffile):
fobj = open(giffile, 'rb')
giftype = fobj.read(6)
pf = struct.unpack('<hhBBB', fobj.read(7))[2]
if pf & 0x80:
pallete_size = 2 << (pf & 0x07)
fobj.read(3 * pallete_size)
# finished reading header
fsize = os.stat(giffile).st_size
while fobj.tell() != fsize:
mark = ord(fobj.read(1))
if mark == 0x21: # gif extension
label = ord(fobj.read(1))
is_comment = label == 254
# read the extension block
blocksize = ord(fobj.read(1))
while blocksize:
if is_comment:
print fobj.read(blocksize)
else:
fobj.read(blocksize)
blocksize = ord(fobj.read(1))
def main(args):
if len(args) < 2:
print "Usage: %s <gif file> ..." % args[0]
sys.exit(0)
for gif in args[1:]:
extract_comments(gif)
if __name__ == "__main__":
main(sys.argv)
--
-- Guilherme H. Polo Goncalves
More information about the Python-list
mailing list