dividing tuple elements with an int or float

R

royG

hi
i am trying to resize some images.First i'd read the size as a 2
tuple and then i want to divide it by 2 or 4 or 2.5 etc..

suppose
origsz=(400,300)
i want to divide the origsize by 2.5 so i can resize to (160,120)

scale=2.5
how can i get the newsz?
obviously origsz/2.5 won't work ..
thanks
RG
 
S

Steven D'Aprano

suppose
origsz=(400,300)
i want to divide the origsize by 2.5 so i can resize to (160,120)

scale=2.5
how can i get the newsz?
obviously origsz/2.5 won't work ..

newsz = (origsz[0]/scale, origsz[1]/scale)
 
J

Jerry Hill

suppose
origsz=(400,300)
i want to divide the origsize by 2.5 so i can resize to (160,120)

scale=2.5
how can i get the newsz?
obviously origsz/2.5 won't work ..

newsz = (origsz[0]/scale, origsz[1]/scale)

That works fine for a 2-tuple, but might get unwieldy for larger
tuples, or if you don't know the length until runtime. A more general
solution might use a generator expression, like this:

newsz = tuple(x/scale for x in origsz)
 
C

castironpi

 > suppose
 > origsz=(400,300)
 > i want to divide the origsize by 2.5 so i can resize to (160,120)
 > scale=2.5
 > how can i get the newsz?
 > obviously origsz/2.5 won't work  ..
 newsz = (origsz[0]/scale, origsz[1]/scale)

That works fine for a 2-tuple, but might get unwieldy for larger
tuples, or if you don't know the length until runtime.  A more general
solution might use a generator expression, like this:

newsz = tuple(x/scale for x in origsz)

You want to perform a uniform call on the elements of a collection.
"Diagram A" appends 0 to every item of a list.
y= [ [] for k in range( 10 ) ]
def x( fun, *ar, **kw ):
... def post( *xr, **xw ):
... return fun( *(xr+ ar), **kw )
... return post
...
x( list.append, 0 )( y[0] )
y [[0], [], [], [], [], [], [], [], [], []]
x( list.pop, 0 )( y[0] ) 0
y [[], [], [], [], [], [], [], [], [], []]
list( map( x( list.append, 0 ), y ) ) [None, None, None, None, None, None, None, None, None, None]
y
[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]

If the elements are immutable,
y= [ () for k in range( 10 ) ]
list( map( x( tuple.__add__, ( 0, ) ), y ) )
[(0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,), (0,)]

you get your new list from map. Here it is with integer elements:
(301, 401)

--You could spell the call like this:
latemap( y, int.__add__, 1 )

With mul:
(600, 800)

It's like partial, but the argument addition is commuted. The key is
that 'y' is going in the first slot, as 'self'. It's not clear that
argument addition commution applies (matches, is correct, generalizes
with right meaning): you just want the first parameter to come from a
separate call. Other uses cases of the generalizer might not align.
+ a fraction on 'x'.

Floats get harder, since you're looking at composition.
(120.0, 160.0) # (160, 120)

+1 compose.

def compose( *f ):
def post( *ar, **kw ):
if len( f )> 1:
return f[0]( compose( *f[1:] )( *ar, **kw ) )
return f[0]( *ar, **kw )
return post
 
C

castironpi

On Wed, 19 Mar 2008 23:06:44 -0700, royG wrote:
 > suppose
 > origsz=(400,300)
 > i want to divide the origsize by 2.5 so i can resize to (160,120)
 > scale=2.5
 > how can i get the newsz?
 > obviously origsz/2.5 won't work  ..
 newsz = (origsz[0]/scale, origsz[1]/scale)
That works fine for a 2-tuple, but might get unwieldy for larger
tuples, or if you don't know the length until runtime.  A more general
solution might use a generator expression, like this:
newsz = tuple(x/scale for x in origsz)

You want to perform a uniform call on the elements of a collection.
+1 compose.

By the way. -And sorry for interrupting the OP's time- I feel that
control flow objects could be a really powerful addition to a
language. Compose a for-loop.
 
G

Gabriel Genellina

A more general
solution might use a generator expression, like this:
newsz = tuple(x/scale for x in origsz)

You want to perform a uniform call on the elements of a collection.
"Diagram A" appends 0 to every item of a list.
y= [ [] for k in range( 10 ) ]
def x( fun, *ar, **kw ):

...     def post( *xr, **xw ):
...             return fun( *(xr+ ar), **kw )
...     return post
...>>> x( list.append, 0 )( y[0] )

Sane programmers replace that crazyness with this code:

for x in collection:
x.append(0)
(301, 401)

Sane programmers replace that crazyness with this code:

tuple(x+1 for x in y)
Floats get harder, since you're looking at composition.


(120.0, 160.0) # (160, 120)

Sane programmers -like D'Aprano, Jerry Hill and me- replace that
crazyness with this code:

tuple(x/2.5 for x in y)
def compose( *f ):
    def post( *ar, **kw ):
            if len( f )> 1:
                    return f[0]( compose( *f[1:] )( *ar, **kw ) )
            return f[0]( *ar, **kw )
    return post

Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.
 
C

castironpi

Sane programmers replace that crazyness with this code:

tuple(x+1 for x in y)

Sane programmers -like D'Aprano, Jerry Hill and me- replace that
crazyness with this code:

tuple(x/2.5 for x in y)

Sane programmers don't write such semi-functional things (unless it
helps expressing the problem in certain domains).
I now think that deprecating map, lambda & Co. was a good thing after
all.

If you write it that way the first time, you need therapy. Actually,
at this point, I (for one, personally) want to investigate 'certain
domains'. Tell me it's really bad at everything or what it's good
at. What can I respect about it?
 
L

Lie

hi
i am trying to resize some images.First i'd read the size as a 2
tuple  and then i want to divide it by 2 or 4 or 2.5 etc..

suppose
origsz=(400,300)
i want to divide the origsize by 2.5 so i can resize to (160,120)

There are several ways to do what you wanted:

-------------
width = origsz[0] * scale
height = origsz[1] * scale
return width, height
-------------
width, height = origsz
return width * scale, height * scale
-------------
return origsz[0] * scale, origsz[1] * scale
-------------
# and an overly complex way of doing this
return tuple(x * scale for x in origsz)
-------------

scale=2.5
how can i get the newsz?
obviously origsz/2.5 won't work  ..
thanks
RG

Aside: I think you're a bit confused about the semantic of scaling in
image resizing, when you want to halve the image size (e.g. 100x100 ->
50x50) you scale it by 0.5, and when you want to double the image size
(e.g. 100x100 -> 200x200) you scale it by 2. This operation is done by
multiplication, not division. (i.e. scaling a 400x300 images by 2.5
means upsizing the image into 1000x750)

That works fine for a 2-tuple, but might get unwieldy for larger
tuples, or if you don't know the length until runtime. A more general
solution might use a generator expression, like this:
(snip)

I think since the semantic of origsz is well defined (width-height
pair of an image) it will always be a 2-tuple, anything other than 2-
tuple should either be discarded or raise an error (depending on
design choice).

P.S.: If you're sure that you want to use division, make sure to "from
__future__ import division" or convert the scaling factor into floats
or you'll most likely get the wrong result as Python defaults to
integer division (due to be changed in Python 3). And you should
remember that the resulting image size should also be (integer,
integer) since you can't have an image that's 100.2432x392.9875
 
L

Lie

If you write it that way the first time, you need therapy.  Actually,
at this point, I (for one, personally) want to investigate 'certain
domains'.  Tell me it's really bad at everything or what it's good
at.  What can I respect about it?

If you (castiro..) write it your way, you'll surely win the Obfuscated
Python Code Contest.
 
C

castironpi

If you (castiro..) write it your way, you'll surely win the Obfuscated
Python Code Contest.

Sometimes you're on a roll and you don't want to back up a step. What
are good "on a roll" habits? (Or, what roll?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top