By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

scaling

P: n/a
hi,
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.

thanks in advance for any help
____
AGK

May 1 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
En Tue, 01 May 2007 11:22:28 -0300, Ashok <as******@gmail.comescribió:
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
May 1 '07 #2

P: n/a
Gabriel Genellina wrote:
[snip]
if x<minvalue: yield 0
elif x>maxvalue: yield top
else: yield (x-minvalue)*top/(maxvalue-minvalue)
[snip]

Personally, I find

yield min(top,max(0,(x-minvalue)*top/(maxvalue-minvalue)))
or
scaled_value = (x-minvalue)*top/(maxvalue-minvalue)
yield min(top,max(0,scaled_value))

clearer, but I am aware that others disagree with this.
Charles
May 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.