numarray types and PIL modes, revisited
Perry Greenfield wrote:
Edward Jones writes:
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 MODULE.NUMARRAY. ---- """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) ----
participants (2)
-
Edward C. Jones
-
Todd Miller