Skip to content

Instantly share code, notes, and snippets.

@skakri
Last active December 22, 2015 05:49
Show Gist options
  • Save skakri/6426697 to your computer and use it in GitHub Desktop.
Save skakri/6426697 to your computer and use it in GitHub Desktop.
xkcd time twitter profile image changer (in action - https://twitter.com/skakri)
import Image
import ImageChops
from twython import Twython
with open('current.txt', 'r+') as f:
index = int(f.read().strip())
new_index = index + 1
f.seek(0)
f.truncate(0)
f.write(str(new_index))
f.close()
with open('sequence.txt') as f:
lines = f.readlines()
lines = map(lambda s: s.strip(), lines)
f.close()
file_one, file_two = lines[index:index + 2]
im1 = Image.open('images/' + file_one)
im2 = Image.open('images/' + file_two)
diff = ImageChops.difference(im2, im1)
left, top, right, bottom = diff.getbbox()
width = right - left
height = bottom - top
side = max(width, height)
halfside = side / 2
hcenter = left + width / 2
vcenter = top + height / 2
cropped = im2.crop((hcenter - halfside, vcenter - halfside, hcenter + halfside, vcenter + halfside))
cropped.save('current.jpg')
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
avatar = open('current.jpg', 'rb')
twitter.update_profile_image(image=avatar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment