Fun with Numbers


>>> n = ""
>>> for x in range(1,10):
... n = n+str(x)
... print "%s x 8 + %d = %d" % (n,x,(int(n) * 8 + x))
...
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
>>>
>>> n = ""
>>> for x in range(1,10):
... n = n+str(x)
... print "%s x 9 + %d = %d" % (n,(x+1),(int(n) * 9 + x+1))
...
...
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
12345678 x 9 + 9 = 111111111
123456789 x 9 + 10 = 1111111111
>>>
>>>
>>>
>>>
>>>
>>> n = ""
>>> r = range(2,10)
>>> r.reverse()
>>> for x in r:
... n = n+str(x)
... print "%s x 9 + %d = %d" % (n,(x-2),(int(n) * 9 + (x-2)) )
...
9 x 9 + 7 = 88
98 x 9 + 6 = 888
987 x 9 + 5 = 8888
9876 x 9 + 4 = 88888
98765 x 9 + 3 = 888888
987654 x 9 + 2 = 8888888
9876543 x 9 + 1 = 88888888
98765432 x 9 + 0 = 888888888

Square thumbnail with Python Image Library

Every once in a while someone will walk up to me and ask, “how do you make a square thumbmail using PIL (Python Image Library)”? To this I say:


import Image

THUMB_SIZE = 80, 80
img = Image.open("someimge.jpg")
width, height = img.size

if width > height:
delta = width - height
left = int(delta/2)
upper = 0
right = height + left
lower = height
else:
delta = height - width
left = 0
upper = int(delta/2)
right = width
lower = width + upper

img = img.crop((left, upper, right, lower))
img.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
img.save("someimge-thumb.jpg")

They usually walk away satisfied…