[Minimum configuration] Use the pillow module of python to synthesize the characters "100 yen" with the image of tapioca milk tea.

Purpose

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.

MicrosoftTeams-image.png

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.

Installation

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.

Draw text on an existing image

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

Application (centering, etc.)

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.

Try to get the image size

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.

Start drawing from the center of the entire image

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.

new_sample.png

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

Furthermore, the size of the drawing area of "100 yen" is acquired, and the place is pulled back by half the value.

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)

Completion code

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.

new_sample.png

Summary

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

Recommended Posts

[Minimum configuration] Use the pillow module of python to synthesize the characters "100 yen" with the image of tapioca milk tea.
I tried to find the entropy of the image with python
Minimum knowledge to get started with the Python logging module
How to crop the lower right part of the image with Python OpenCV
The easiest way to synthesize speech with python
Specify the Python executable to use with virtualenv
Use Pillow to make the image transparent and overlay only part of it
The easiest way to use OpenCV with python
How to identify the element with the smallest number of characters in a Python list?
A memo of misunderstanding when trying to load the entire self-made module with Python3
How to use the Raspberry Pi relay module Python
Convert the image in .zip to PDF with Python
Let's use the Python version of the Confluence API module.
Return the image data with Flask of Python and draw it to the canvas element of HTML
I made a function to crop the image of python openCV, so please use it.
Why can I use the module by importing with python?
Make the display of Python module exceptions easier to understand
[Python] I want to use the -h option with argparse
Try to automate the operation of network devices with Python
Get the source of the page to load infinitely with python.
[Python] I tried to visualize the prize money of "ONE PIECE" over 100 million characters with matplotlib.
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
Made it possible to convert PNG to JPG with Pillow of Python
[Python] Try to graph from the image of Ring Fit [OCR]
[September 2020 version] Explains the procedure to use Gmail API with Python
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Sample to use after OAuth authentication of BOX API with Python
Consider the speed of processing to shift the image buffer with numpy.ndarray
[Python] Explains how to use the format function with an example
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
The story of making a module that skips mail with python
Create a compatibility judgment program with the random module of python.
PhytoMine-I tried to get the genetic information of plants with Python
The story of making a tool to load an image with Python ⇒ save it as another name
Python: How to use async with
How to use the optparse module
How to use FTP with Python
How to use the ConfigParser module
The story of making a university 100 yen breakfast LINE bot with Python
[Python] Explains how to use the range function with a concrete example
Use python on Raspberry Pi 3 to light the LED with switch control!
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
I tried to get the authentication code of Qiita API with Python.
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
I tried to streamline the standard role of new employees with Python
The result of making the first thing that works with Python (image recognition)
[Introduction to Python] What is the method of repeating with the continue statement?
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV