Skip to content

Instantly share code, notes, and snippets.

@segfaultlabs
Forked from Akkiesoft/profile-image.py
Created August 27, 2017 06:37
Show Gist options
  • Save segfaultlabs/8fb4352f710a0452a1c323d3c0a2c4cd to your computer and use it in GitHub Desktop.
Save segfaultlabs/8fb4352f710a0452a1c323d3c0a2c4cd to your computer and use it in GitHub Desktop.
ツイッターの情報で名札画像を作る(電子ペーパー向け)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import tweepy
import urllib3
from StringIO import StringIO
from PIL import Image,ImageDraw,ImageFont,ImageOps
import textwrap
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
palette = [
255, 255, 255,
0, 0, 0,
]
def get_icon(url):
http = urllib3.PoolManager()
r = http.request('GET', url)
icon = Image.open(StringIO(r.data))
if icon.mode == 'RGBA':
r,g,b,a = icon.split()
icon = Image.merge('RGB', (r,g,b))
icon.thumbnail((80,80))
icon = ImageOps.invert(icon)
return icon.convert("1")
def create_profile(sn, username, icon_url):
profile = Image.new("P", (250,122), 0)
profile.putpalette(palette)
draw = ImageDraw.Draw(profile)
fontpath = "/usr/share/fonts/truetype/vlgothic/VL-PGothic-Regular.ttf"
icon = get_icon(icon_url)
icon_w, icon_h = icon.size
profile.paste(icon, (5, 122 / 2 - icon_h / 2))
sy = 15
fontsize = 20
draw.font = ImageFont.truetype(fontpath, fontsize)
for line in textwrap.wrap(username, width=7):
draw.text((90, sy), line, 1)
sy += fontsize
sy += 10
for line in textwrap.wrap("@" + sn, width=10):
draw.text((90, sy), line, 1)
sy += fontsize
profile.save(sn + ".png")
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
user = api.get_user("@Akkiesoft")
create_profile(
user.screen_name,
user.name,
user.profile_image_url.replace("_normal.", ".")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment