[Image-SIG] Re: Question about file decoder

Fredrik Lundh fredrik@pythonware.com
Thu, 8 May 2003 13:19:59 +0200

Volker Dobler wrote:

> I wrote a file decoder for a simple image format
> (header followed by 16bit data) by subclassing
> ImageFile, providing a _open() method and using
> a the apropriate raw decodeer.  It works.
> Now my question:  The 16bit data is scaled by a
> factor and should be rescaled to the "real" values
> during reading.  The scaling factor can be found
> in the header and is extracted during _open().
> How can I do this rescaling during the reading of
> the file.
> Of course a can save the scaling factor and do a
> img.point( lambda z: z*scaling + 0 ) afterwards,
> but I want to implement this step into the reading
> of the data.

you can override the "load_end" method in your ImagePlugin, and
replace or manipulate the pixel storage in there.

this method is called when the pixel data has been loaded (which
is usually done when the data is first accessed), but before any
other function gets around to access the data.

something like this should work:

    class MyImagePlugin(ImageFile):

        def _open(self):
            self.scale = getscale()

        def load_end(self):
            new = Image.point(lambda z: z*self.scale + 0)
            self.im = new.im # replace storage

alternatively, you can use (undocumented, inofficial) methods on
the storage object:

        def load_end(self):
            self.im = self.im.point_transform(self.scale, 0)