Image characters and post to slack (python slackbot)

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

Install Pillow

pip install Pillow

Script to image characters and post to slack

# -*- 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 ")

I don't understand well

--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.

Other

――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

Image characters and post to slack (python slackbot)
Post from Python to Slack
Post to vim → Python → Slack
Post to slack with Python 3
Post to Slack in Python
Post an article with an image to WordPress with Python
Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)
Try to make BOT by linking spreadsheet and Slack with python 1/2 (python + gspread + slackbot)
POST messages from python to Slack via incoming webhook
Post to Slack via Subscriber
Post to Twitter using Python
Try using Junos' On-box Python #Bonus 1 Something like ChatOps / Commit and post to Slack
Send experiment results (text and images) to slack with Python
[Let's play with Python] Image processing to monochrome and dots
[Introduction to Python3 Day 1] Programming and Python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Workflow to convert formula (image) to python
Post to slack in Go language
Post images from Python to Tumblr
Python logging and dump to json
Selenium and python to open google
Easily post to twitter with Python 3
[Nanonets] How to post Memo [Python]
Introduction to image analysis opencv python
Post a message from IBM Cloud Functions to Slack in Python
[Python] Try to recognize characters from images with OpenCV and pyocr
How to package and distribute Python scripts
From Python to using MeCab (and CaboCha)
HTML email with image to send with python
How to install and use pandas_datareader [Python]
Output color characters to pretty with python
Bind methods to Python classes and instances
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Send a message from Python to Slack
POST variously with Python and receive with Flask
Fractal to make and play with Python
[Python] Create a linebot to write a name and age on an image
Log in to Slack using requests in Python
Porting and modifying doublet-solver from python2 to python3.
Read Python csv and export to txt
python: How to use locals () and globals ()
POST photos with Microsoft Bing Image Search API to get Image Insights (Python)
How to adjust image contrast in Python
Build a Postfix mail server, start Python code triggered by mail reception, and post mail to Slack (local environment)
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
Compress python data and write to sqlite
[python] Send the image captured from the webcam to the server and save it
How to use is and == in Python
Sample to convert image to Wavelet with Python
Send a message from Slack to a Python server
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Convert PDF to image (JPEG / PNG) with Python
How to generate permutations in Python and C ++
[Vim] [Python] Bugs around jedi-vim and multibyte characters?
Generate and post dummy image data with Django
8-bit grayscale Downscaling to images and image contrast
How to use Python Image Library in python3 series
How to crop an image with Python + OpenCV