# -*- coding: utf-8 -*-
from PIL import Image,ImageDraw,ImageFont
from bs4 import BeautifulSoup
import sys
g_temppng='tempclip.png'
def cal_text_length(text):
en_text_length=13
zh_text_length=18
zh_text_size=3
offset=8
if BeautifulSoup(text).originalEncoding=='utf-8':
w=zh_text_length*text.__len__()/zh_text_size+offset
else:
w=en_text_length*text.__len__()
return w
def generate_image(text,background_color,fill_color):
height=128
font_size=18
w=cal_text_length(text)
width=w+50
img=Image.new('RGB',(width,height),background_color)
draw=ImageDraw.Draw(img)
text_to_draw=unicode(text,'utf-8')
font = ImageFont.truetype('/Library/Fonts/华文黑体.ttf', font_size)
draw.text(((width - w) / 2, (height - font_size) / 2), text_to_draw, font=font, fill=fill_color)
del draw
img.save(g_temppng)
if __name__ == '__main__':
text="no input"
if len(sys.argv)>1:
text=sys.argv[1]
generate_image(text,'#000000','#ffffff')

716

被折叠的 条评论
为什么被折叠?



