[Image-SIG] PIL: how to subclass Image?

William Henney whenney at gmail.com
Wed Mar 29 02:06:42 CEST 2006


Dear list,

I was wondering how one should best go about subclassing PIL's image
class. This does not seem as straightforward as I thought it was going
to be. I have managed to get something to work as follows:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import Image
class MyImage(Image.Image):
    """
    Helper class to create empty image of desired shape
    """
    def __init__(self, mode='L', size=None):
        Image.Image.__init__(self)
        self.mode = mode
        self.size = size
        self.im = Image.core.new(self.mode, self.size)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

which I then can subclass something like this (the details aren't
important):

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class FitsImage(MyImage, FitsData):
    """
    A fits file with associated grayscale image, with gamma-law
    scaling between fmin and fmax.
    """
    def __init__(self, <ARGLIST>):
        FitsData.__init__(self, <SOME_ARGS>)
        MyImage.__init__(self, 'L', self.size)
        <SOME_CALCULATIONS>
	self.putdata(self.flatnormaldata, scale=255.0)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

and then it gets used something like this:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
im = FitsImage(fitsfilename, fmin, fmax)
im.transpose(Image.FLIP_TOP_BOTTOM).save("whatever.png")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

My main worry is how robust this is to future development of PIL. Is
Image.core.new() supposed to be part of the API? Is there a better way
of doing this? Are there any plans to give a fully OO interface to PIL
in the future?

Thanks in advance for any advice,

Will 

--

  Dr William Henney, Centro de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia



More information about the Image-SIG mailing list