Pythons & Snow Bunnies

Around this time last winter, I was learning how to write code in the computer language, Python.  I thought that knowing how to code would make me a better librarian, and so I signed up for a grad class at the U of I.  For the record, I hadn’t cried because of a class since the fifth grade, when Mrs. Recinos gave me a “late” because I forgot to ask my parents to sign my assignment notebook.  But Readers, Python made me cry.

Eventually, though, I ended up with this cute little piece of code that can make collages out of pictures that you like:

Snow Bunny Collage

My professor was really amazing, and in the end I actually did OK in the class.  Having a supportive, code-savvy fiance with a knack for soothing hysterical people also helped.  But librarians — even though I know I couldn’t crank out a Python program on the spot today if my life depended on it, I do know that I could sit down with a text book for a few hours and figure it out, and that I am also now equipped to have intelligent conversations with library IT staff who write code for the library.  It’s nice to know that I can participate in building our library’s tools.

And now, for your reading pleasure, a little hunk o’ Python:

def createCollage(a, b):
myCollage = media.create_picture(a, b)
return myCollage

def addPictureToCollage(picture, collage, x, y, scaleFactor):
oldPic = media.load_picture(picture)
oldPicWidth = media.get_width(oldPic)
oldPicHeight = media.get_height(oldPic)
newPicHeight = int(media.get_height(oldPic) * scaleFactor)
newPicWidth = int(media.get_width(oldPic) * scaleFactor)
newPic = media.create_picture(newPicWidth, newPicHeight)
for row in range (0, newPicWidth):
for col in range (0, newPicHeight):
oldPicColor = media.get_pixel(oldPic, (int((row * oldPicHeight)/newPicHeight)), (int((col*oldPicWidth)/newPicWidth)))
newPicColor = media.get_pixel(newPic, row, col)
media.set_color(newPicColor, oldPicColor)
for row in range(0, newPicWidth):
for col in range (0, newPicHeight):
pixelcolor = media.get_pixel(newPic, row, col)
collagepixel = media.get_pixel(collage, row + x, col + y)
media.set_color(collagepixel, pixelcolor)
return collage

def rotatePictureClockwise(pic):
picObject = media.load_picture(pic)
picHeight = media.get_height(picObject)
picWidth = media.get_width(picObject)
newPic = media.create_picture(picHeight, picWidth)
for row in range (0, picWidth):
for col in range (0, picHeight):
from_pixel = media.get_pixel(picObject, row, col)
to_pixel = media.get_pixel(newPic, picHeight – (col + 1), row)
media.set_color(to_pixel, from_pixel)
return newPic

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s