It is a script that images and posts the characters of hubot that I wrote last time (although it was a long time ago), Recently, there are many opportunities to touch Python, I think python slackbot is easy to use, so I rewrote it with python
With python, instead of not having to include ImageMagic, you need to include Pillow in pip
Python is using 3 or later, Depending on the environment, the following command may be pip3 instead of pip
pip install Pillow
# -*- coding: utf-8 -*
from slackbot.bot import respond_to
from slackbot.bot import listen_to
from slackbot.bot import default_reply
from PIL import Image, ImageDraw, ImageFont
import random
@respond_to('str_img\s+(.*)')
def mention_func1(message, arg1):
spcolor = [
'magenta',
'orange',
'LimeGreen',
'blue',
'purple',
'OrangeRed',
'SkyBlue',
'LightBlue',
'Turquoise',
'gold'
]
im = Image.new("RGB",(50,50),"white")
fnt = ImageFont.truetype('/slackbot/plugins/cp_font.ttf', 50)
draw = ImageDraw.Draw(im)
text_size = draw.textsize(arg1, fnt)
im = im.resize(text_size)
draw = ImageDraw.Draw(im)
draw.text((0,0), arg1, fill=random.choice(spcolor), font=fnt)
im.save("/tmp/str_img.png ")
message.channel.upload_file(fname="/tmp/str_img.png ", fpath="/tmp/str_img.png ")
--ImageDraw.Draw twice to add characters and create an image for the width of the characters --I'm not familiar with python (especially related to images), so if there is another good way to write it, I'll fix it.
――I had the option to change the image size last time, but I omitted it this time because it will be the same size when it is displayed in slack
Recommended Posts