[PYTHON] Convert a string to an image

Overview

Using Python and PIL, I summarized how to output an image with the character string drawn from the character string. I just prepare a background image and draw characters.

PIL PIL (Python Image Library) is a Python image processing library. It seems that development has stopped now, so it would be better to use the successor Pillow, but Anaconda has PIL installed as standard, and it was possible to realize with the PIL function. Therefore, PIL is used.

Image output

I couldn't find a page with the title of converting a character string to an image, so I used it after investigating how to draw characters while looking at how to use the image processing library. In the first place, I don't think there is a demand for converting character strings to images, but I think there are small uses such as converting email addresses to images as a measure against spam. Therefore, the title was "Converting a character string to an image".

In the work, I referred to How to use Python's image processing library Pillow (PIL).

After creating an Image object with PIL.Image.new (), create a Draw object with PIL.ImageDraw.Draw (). In PIL.Image.new (), the color mode, image size, and background color are specified as arguments.

Specify the font used in PIL.ImageFont.truetype and its size. After that, I got the size (number of pixels) of the text to be drawn with draw.textsize (), adjusted the text so that it was in the desired place, and drew it with draw.text ().

import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont

#Settings for font, size, and text to draw
ttfontname = "C:\\Windows\\Fonts\\meiryob.ttc"
fontsize = 36
text = "Implicit type declaration"

#Set image size, background color, font color
canvasSize    = (300, 150)
backgroundRGB = (255, 255, 255)
textRGB       = (0, 0, 0)

#Creating an image to draw characters
img  = PIL.Image.new('RGB', canvasSize, backgroundRGB)
draw = PIL.ImageDraw.Draw(img)

#Draw a character string on the prepared image
font = PIL.ImageFont.truetype(ttfontname, fontsize)
textWidth, textHeight = draw.textsize(text,font=font)
textTopLeft = (canvasSize[0]//6, canvasSize[1]//2-textHeight//2) #1 from the front/6, placed in the center of the top and bottom
draw.text(textTopLeft, text, fill=textRGB, font=font)

img.save("image.png ")

Output image

image.png

The image was output safely.

Recommended Posts

Convert a string to an image
Try to extract a character string from an image with Python3
Convert hexadecimal string to binary
[python] Convert date to string
I want to convert an image to WebP with lollipop
How to convert / restore a string with [] in python
What do you like about how to convert an array (list) to a string?
How to convert an array to a dictionary with Python [Application]
Convert A4 PDF to A3 every 2 pages
Workflow to convert formula (image) to python
Convert a path string that uses a symbolic link in the middle to an absolute path
I want to convert an ISO-8601 character string to Japan time
Convert the image data (png) at hand to a .pbm image
Convert PDF to image with ImageMagick
A tool to convert Juniper config
Convert to a string while outputting standard output with Python subprocess
How to make a string into an array or an array into a string in Python
[Python] How to convert a 2D list to a 1D list
Convert (compress) formatted JSON string to 1-line JSON
Try to generate an image with aliasing
An Introduction to Object-Oriented-Give an object a child.
Sample to convert image to Wavelet with Python
Convert to HSV
Note: [Python3] Convert datetime to a string in any format you like
I want to color a part of an Excel string in Python
Use BeautifulSoup to extract a link containing a string from an HTML file
How to code a drone using image recognition
Convert PDF to image (JPEG / PNG) with Python
How to embed a variable in a python string
How to create a function object from a string
I tried to get an image by scraping
Convert a multidimensional list (array) to one dimension
Python Note: When assigning a value to a string
I tried to generate a random character string
Write a script to convert a MySQL dump to TSV
Insert an object inside a string in Python
Post an article with an image to WordPress with Python
Convert pandas dataframe elements to regular string type
I tried to extract a line art from an image with Deep Learning
Parse a JSON string written to a file in Python
I want to embed a variable in a Python string
Convert a slice object to a list of index numbers
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert a text file with hexadecimal values to a binary file
How to convert a class object to a dictionary with SQLAlchemy
Convert jupyter to py
[Python] How to expand variables in a character string
Convert keras-yolo3 to onnx
Save the graph drawn by pyqtgraph to an image
Convert the image in .zip to PDF with Python
I made a code to convert illustration2vec to keras model
How to turn a .py file into an .exe file
Convert dict to array
I want to split a character string with hiragana
How to convert a mel spectrogram back to a wav file
How to create an image uploader in Bottle (Python)
How to deploy a Go application to an ECS instance
[Python] Created a method to convert radix in 1 second
Convert json to excel
How to take a captured image from a video (OpenCV)