Create a gist now

Instantly share code, notes, and snippets.

@nyarurato /meisi.py
Last active Aug 29, 2015

What would you like to do?
# -*- coding:utf-8 -*-
import Twitter #自作twitterAPIライブラリ
import Dither #画像2値化自作ライブラリ
from PIL import Image,ImageDraw,ImageFont
from urllib import urlretrieve
from os import mkdir,path,linesep
from qrcode import QRCode
from urllib2 import urlopen
from sys import stdout
from printer import ThermalPrinter
#ユーザーの情報を取得
def info_get():
twitter = Twitter.Twitter()
stdout.write("input Twitter ID :@")
data = twitter.get_user_info(raw_input())
return data
#ユーザーの画像を保存するディレクトリ作成
def make_user_dir(data):
if(not path.exists("user")):
mkdir("user")
name_dir = path.join("user",data["screen_name"])
if(not path.exists(name_dir)):
mkdir(name_dir)
return name_dir
#名刺画像作成
def make_img(data):
user_dir = make_user_dir(data)
icon_url = data["profile_image_url"].replace("_normal","")
ext = path.splitext(icon_url)[1]
icon_path = path.join(user_dir,"icon"+ext)
print("get user icon...")
#Get Twitter Icon
urlretrieve(icon_url,icon_path)
print("get user icon complete!")
resouce_path = "resouce"
#Twitter Icon
#2値化
icon2_path = path.join(user_dir,"icon2.png")
dither = Dither.Dither(icon_path)
dither_icon = dither.error_diffusion()
dither_icon.save(icon2_path,"PNG")
#QRコード生成
qr = QRCode(version=1, box_size=6,border=0)
qr.add_data("http://twitter.com/"+data["screen_name"])
qr.make(fit=True)
qrimg = qr.make_image().resize((128,128))
#small icons
tw_icon_img = Image.open(path.join(resouce_path,"twitter.png")).resize((25,25))
loca_icon_img = Image.open(path.join(resouce_path,"location.png")).resize((15,15))
web_icon_img = Image.open(path.join(resouce_path,"web.png")).resize((15,15))
img = Image.new("1", (634,383),"white") #634x383の画像を作成
draw = ImageDraw.Draw(img)
make_txt_img(data,draw)
  #画像配置
img.paste(dither_icon,(15,15))
img.paste(qrimg,(15,220))
img.paste(tw_icon_img,(170,70))
img.paste(loca_icon_img,(170,107))
img.paste(web_icon_img,(170,132))
img = img.rotate(90,expand=1) #印刷用に90度回転
final_img_path = path.join(user_dir,"meisi.png")
img.save(final_img_path,"PNG")
print("generate user meisi img!!")
return img
# 文字の画像化,ついでに配置
def make_txt_img(data,draw):
fontpath = path.join("resouce","GenShinGothic-P-Light.ttf")
theme_font = ImageFont.truetype(fontpath,40,encoding='unic')
description_font = ImageFont.truetype(fontpath,20,encoding='unic')
draw.text((200,60),"@"+data["screen_name"],font= theme_font)
draw.text((170,20),data["name"],font= theme_font)
if(data["location"] == ""):
draw.text((190,105),u"NT金沢2015 @金沢駅",font= description_font)
else:
draw.text((190,105),data["location"],font= description_font)
if(data["url"] == None):
draw.text((190,130),"No Web Site",font= description_font)
else:
web_url = urlopen(data["url"]).geturl()
web_url = txt_boxing(web_url,description_font,190,130,570,150)
draw.text((190,130),web_url,font= description_font)
words = txt_boxing(data["description"],description_font,150,230,570,300)
draw.text((170,220),words,font= description_font)
#文字列の改行入れ
def txt_boxing(words,font,startx,starty,maxx,maxy):
linesize = 0
box = (maxx-startx,maxy-starty)
boxwords = ""
wordsize = 0
for word in words:
wordsize = font.getsize(word)[0]
if (linesize + wordsize) >= box[0]:
linesize = wordsize
boxwords += "\n" + word
else:
linesize += wordsize
boxwords += word
return boxwords
def main():
serialport = ThermalPrinter.SERIALPORT
thprinter = ThermalPrinter(serialport=serialport)
while(True):
print(u"\n***Twitter Meisi Print Service***")
data = info_get()
imgpath = make_img(data)
thprinter.print_img(imgpath)
thprinter.linefeed()
thprinter.linefeed()
thprinter.linefeed()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment