[Numpy-discussion] numarray types and PIL modes, revisited

Edward C. Jones edcjones at erols.com
Wed Jan 1 20:29:44 EST 2003

Perry Greenfield wrote:
 > Edward Jones writes:

 > > I write code using both PIL and numarray. PIL uses strings for
 > > modes and numarray uses (optionally) strings as typecodes. This
 > > causes problems.  One fix is to emit a DeprecationWarning when
 > > string typecodes are used.  Two functions are needed:
 > > StringTypeWarningOn and StringTypeWarningOff.  The default
 > > should be to ignore this warning.
 > I'm not sure I understand. Can you give me an example of problem
 > code or usage? It sounds like you are trying to test the types of
 > PIL and numarray objects in a generic sense. But I'd understand
 > better if you could show an example.

That's what I was thinking (incorrectly). But I don't need to directly 
compare PIL modes with numarray types.

My code never tries to deduce whether an array is a numarray or a PIL 
image from just the natype_or_mode. A module name (MODULE.NUMARRY, 
MODULE.PIL) must also be given. I do things this way because I might 
want to include other array/image systems. In an earlier version, I had 
a MODULE.IPL for the Intel Image Processing Library.

The code also implements a policy of forbidding string types.

So now all I can say is:

1. UInt8 == 'X' should not raise an exception. It should return False.

3. There needs to be a function that returns True iff arg is a numarry 
type (UInt8, "UInt8", "b", ...).

def IsType(rep):
     from numerictypes import typeDict
     return isinstance(rep, NumericType) or typeDict.has_key(rep)

Here is a typical piece of code. "module" can be MODULE.PIL or

"""General image casting function. Changes the C type of the pixels. 
Information can be lost. The "Convert" functions call C casting 
functions that clip the values, For example, if the input is a UInt16 
and the output is a Int16, any input value greater than 32767 becomes 32767.
def ArrayToArrayCast(arrin, module, natype_or_mode):
     """Converts one array into another. Results are clipped."""
     pars = Parameters(arrin)
     if pars.module == module == MODULE.PIL and \
           pars.mode == natype_or_mode:
         return arrin
     if pars.module == module == MODULE.NUMARRAY and \
                      NA_SameType(pars.natype, natype_or_mode):
         return arrin
     if pars.module == MODULE.NUMARRAY and module == MODULE.NUMARRAY:
         return NA_To_NA_Convert(arrin, natype_or_mode)
     if pars.module == MODULE.PIL and module == MODULE.PIL:
         return PIL_To_PIL_Convert(arrin, natype_or_mode)
     if pars.module == MODULE.NUMARRAY and module == MODULE.PIL:
         return NA_To_PIL_Convert(arrin, natype_or_mode)
     if pars.module == MODULE.PIL and module == MODULE.NUMARRAY:
         return PIL_To_NA_Convert(arrin, natype_or_mode)

More information about the NumPy-Discussion mailing list