tags:

views:

131

answers:

6

I have the following code:

print img.size
print 10 * img.size

this will print

(70, 70)
(70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70)

while I'd like it to print

(700, 700)

Is any way there to do this without having to write

print (10 * img.size[0], 10 * img.size[1])

PS: img.size is a PIL image. Dunno if that matters anything in this case.

+2  A: 

Might be a nicer way, but this should work

tuple([10*x for x in img.size])
kigurai
+1  A: 

You can try something like this:

print [10 * s for s in img.size]

It will give you a new list with all the elements you have in the tuple multiplied by 10

Serge
+3  A: 

The pythonic way would be using a list comprehension:

y = tuple([z * 10 for z in img.size])

Another way could be:

y = tuple(map((10).__mul__, img.size))
ChristopheD
That second one is awesome... haha.
Mike Boers
+5  A: 
img.size = tuple(i * 10 for i in img.size)
truppo
+1  A: 

There is probably a simpler way than this, but

print map(lambda x: 10*x, img.size)

Will do nearly what you want, although it prints as a list rather than a tuple. Wrap the map call inside tuple(map...) if you want it to print as a tuple (parentheses rather than square brackets).

sparklewhiskers
+1  A: 

If you have this problem more often and with larger tuples or lists then you might want to use the numpy library, which allows you to do all kinds of mathematical operations on arrays. However, in this simple situation this would be complete overkill.

nikow