The trigger was the rich menu of LINE Bot. This is a bot that I made myself, but if you tap the rich menu at the bottom of the screen, You will get a response that is commensurate with that.
By the way, the point this time is the rich menu part. This is actually a free image, not a button (currently, the LINE system seems to have no choice but to express it as an image ...).
So it's difficult to make this. Unfortunately, I didn't have such a good app, so I cut and paste it using PowerPoint and paint. I think it's a good result, but it took a long time. (Addition: With a recent update, you can easily create it on the LINE Developers screen!)
So I was thinking, "Isn't it possible with Python?" It turns out that it seems possible to do it with a module called pillow! Let's see how to do it right away.
If you are using Python, you are already used to it, but for the time being, install the module.
$ pip install pillow
By the way, pillow seems to be a fork project of a module called PIL. The official documentation is here . This time, I would like to do image processing mainly with reference to this document.
This time, I would like to add "100 yen" to this tapioca image. Suddenly go from the code.
main.py
from PIL import Image, ImageDraw, ImageFont
#Read the original image
image = Image.open("tapioca.png ")
#Since the object for writing characters is prepared, get it
draw = ImageDraw.Draw(image)
#Specify the font (font file is C if Windows 10:\\Windows\\It's in Fonts)
#size is the font size (50 for the time being)
font = ImageFont.truetype("YuGothL.ttc", size=50)
#Draw letters
#the first(0,0)Is the coordinate position to start drawing characters, of course.(10,10)Etc. are OK
#fill determines the color of characters with RGB
draw.text((0, 0), "100 yen", fill=(255,0,0), font=font)
#Save the image
image.save("new_tapioca.png ")
It turned out to be something like this.
I will play with the arrangement of letters. As far as I've seen the docs, I had that option, but for some reason it didn't work, so I figured out a primitive way to do it.
main.py
from PIL import Image, ImageDraw, ImageFont
#Read the original image
image = Image.open("tapioca.png ")
#Get the size of the image
print(image.size[0]) # =>771 in this example
print(image.size[1]) # =>856 in this example
Now you know the maximum size.
If the above 771 and 856 are set to the drawing start position, it will start from the lower right of the image, and you can hardly see the special characters. Now, divide 771 and 856 by 2 and use that position as the starting position. This should start drawing from the center of the image.
I knew it well, but it looks like this. The starting point is certainly in the middle of the image, but it's not like this ...
The title is a little difficult to understand, but I want to align the center of "100 yen" with the center of the entire image. In other words, it can be realized by getting the size when "100 yen" is drawn and then returning the starting point by half the size of "100 yen". Well, it's difficult to put into words.
I will try it for the time being. First of all, I was able to get the size of "100 yen" itself below.
from PIL import Image, ImageDraw, ImageFont
#When reading the original image
image = Image.open("tapioca.png ")
#Since the object for writing characters is prepared, get it
draw = ImageDraw.Draw(image)
#Decide the font
font = ImageFont.truetype("YuGothL.ttc", size=50)
#Get the size of "100 yen"
draw_text_width, draw_text_height = draw.textsize("100 yen", font=font)
First, after setting the drawing start point of "100 yen" as the central part of the original image Pull back up and left by half the size of this draw_text_width and draw_text_height. Think of it as a continuation of the code above.
draw.text(((image.size[0] / 2 - draw_text_width / 2), (image.size[1] / 2 - draw_text_height / 2)), "100 yen", fill=(255, 0, 0), font=font)
It's long and difficult to understand. This may be easier to understand.
start_X_point = image.size[0] / 2 - draw_text_width / 2
start_Y_point = image.size[1] / 2 - draw_text_height / 2
draw.text((start_X_point, start_Y_point), "100 yen", fill=(255, 0, 0), font=font)
main.py
from PIL import Image, ImageDraw, ImageFont
#When reading the original image
image = Image.open("tapioca.png ")
#Since the object for writing characters is prepared, get it
draw = ImageDraw.Draw(image)
#Decide the font
font = ImageFont.truetype("YuGothL.ttc", size=50)
#Get the size of the character you want to draw
draw_text_width, draw_text_height = draw.textsize("100 yen", font=font)
#Determine the coordinates of the drawing start point based on the size of the character you want to draw and the size of the original image
start_X_point = image.size[0] / 2 - draw_text_width / 2
start_Y_point = image.size[1] / 2 - draw_text_height / 2
#draw
draw.text((start_X_point, start_Y_point), "100 yen", fill=(255, 0, 0), font=font)
#Save the finished image
image.save("new_tapioca.png ")
I was happy to bring it to the center.
-Images and characters can be easily combined with Pillow! ・ It's easy to synthesize, but centering may be a little troublesome. (As far as I read the docs, I have the option of centering, but for some reason it doesn't work ...)