scaling
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue May 1 19:07:05 EDT 2007
En Tue, 01 May 2007 11:22:28 -0300, Ashok <ashokagk at gmail.com> escribió:
> IDL language contains a function called BYTSCL to scale all values of
> Array that lie in the range (Min £ x £ Max) into the range (0 £ x £
> Top). Is there a similar function available in python?
>
> I need this to scale the pixel values of an image using PIL.
Maybe PIL contains something built in, but if all else fails, you could
write it in Python:
def bytsclGen(data, minvalue=None, maxvalue=None, top=255):
if minvalue is None: minvalue = min(data)
if maxvalue is None: maxvalue = max(data)
for x in data:
if x<minvalue: yield 0
elif x>maxvalue: yield top
else: yield (x-minvalue)*top/(maxvalue-minvalue)
def bytscl(data, minvalue=None, maxvalue=None, top=255):
return list(bytsclGen(data, minvalue, maxvalue, top))
--
Gabriel Genellina
More information about the Python-list
mailing list