PIL Problem (Python Imaging Library)

Jerome ALET alet at unice.fr
Mon Jan 24 04:41:14 EST 2000


Hi,

Does someone know if the standard PIL library can allow me to 
extract many graphic files (JPEG in my case) from a special undocumented
format 
archive ?

the only thing I know is the offset at which begins each jpeg file, but
I don't know
their sizes, and I don't want to analyze the jpeg format to finally
discover the size (in fact I want PIL to do it for me).

I've made a very small patch to PIL which works and allows me to do the
following (say the jpeg files are all on 256 bytes boundaries):
--------------------
        file = open(filename, "rb")	# the 'b' is only for portability to
winXX
	# ...
        szbuf = 256     # all included documents seem to be on a 256
bytes boundary
        count = 0
        eof = 0
        while not eof :
                jpegpos = file.tell()
                read = file.read(szbuf)
                if len(read) :
                        if isjpeg(read) :	# test if the read chunk
ressembles a jpeg file
                        	count = count + 1
                                outfilename = ("%02i" % count) + '.jpeg'

                                newpos = file.tell()
                                file.seek(jpegpos, 0)

				# I know I'm at the beginning of a graphic file
				# but I don't know the graphic file's size
                                image = Image.open(file)

				# fortunately the save function loads the image in
				# memory before saving, so I don't have to know its size
                                image.save(outfilename, quality = 100)

                                file.seek(newpos, 0)
				print "\t" + outfilename, "\tSize:", image.size
                else :
                        eof = 1
        file.close()
--------------------

but I wanted to know if there's a standard way with PIL to do exactly
this without any patch
(and without knowing the included jpeg filesizes) in order for me to be
able to redistribute my program without worrying people about having the
patched PIL. I suppose if another way exists then it should be without
calling Image.open because this function assumes the graphic file begins
at offset 0 which is false in my case. (the same for the save function).

Please could you CC any answer to my email address sine I don't read
newsgroups very often ?

-- 
Jerome ALET - alet at unice.fr - http://cortex.unice.fr/~jerome



More information about the Python-list mailing list