Make an Image with text with Python

Create an image with PIL

Python is great at many things, expecially for repetitive things. We can use it to gain a lot of time, and we all are aware that today time is the most evaluable thing, because things go faster faster in today’s life.

Related image

A practical example: make an image

In this code we will create images. From scratch. I thought to make this code to use it on the web pages to enphatize something with a nice image of a text. This code will show you a nice red rectangle.

Image.new

This will create a new image with color size and color as attributes.

Image.save

This save the image create with new.

from PIL import Image, ImageDraw
import os

# name of the file to save
filename = "img01.png"

# create new image
image = Image.new(mode = "RGB", size = (200,70), color = "red")

# save the file
image.save(filename)

# open the file
os.system(filename)

Write some text in it

Now we must use the ImageDraw.Draw class of PIL. The istance will be called draw (with a lot of fantasy).

ImageDraw.Draw

We will put as argument of this class the image object created with Image.new.

text

Finally we will use the method text of the ImageDraw.Draw object created to add the text to the image. This takes as arguments the position (a tuple), the text (string) and another tuple for the color.

The complete code

from PIL import Image, ImageDraw, ImageFont
import os

# name of the file to save
filename = "img01.png"
fnt = ImageFont.truetype('arial.ttf', 15)
# create new image
image = Image.new(mode = "RGB", size = (200,70), color = "red")
draw = ImageDraw.Draw(image)
draw.text((10,10), "My Text", font=fnt, fill=(255,255,0))
image.save(filename)

os.system(filename)
The text on the created red image
The text on the created red image

A function to make changes easy

from PIL import Image, ImageDraw, ImageFont
import os

def text_on_img(filename='01.png', text="Hello", size=12):
	"Draw a text on an Image, saves it, show it"
	fnt = ImageFont.truetype('arial.ttf', 52)
	# create image
	image = Image.new(mode = "RGB", size = (200,70), color = "red")
	draw = ImageDraw.Draw(image)
	# draw text
	draw.text((10,10), text, font=fnt, fill=(255,255,0))
	# save file
	image.save(filename)
	# show file
	os.system(filename)


text_on_img(text="Text", size=52)

Let’s adapt the width of the img to the lenght of the text

from PIL import Image, ImageDraw, ImageFont
import os

def text_on_img(filename='01.png', text="Hello", size=12):
	"Draw a text on an Image, saves it, show it"
	fnt = ImageFont.truetype('arial.ttf', size)
	# create image
	image = Image.new(mode = "RGB", size = (int(size/2)*len(text),size+50), color = "red")
	draw = ImageDraw.Draw(image)
	# draw text
	draw.text((10,10), text, font=fnt, fill=(255,255,0))
	# save file
	image.save(filename)
	# show file
	os.system(filename)


text_on_img(text="Text to write on img", size=300)

Let’s make background and color avaiable

from PIL import Image, ImageDraw, ImageFont
import os

def text_on_img(filename='01.png', text="Hello", size=12, color=(255,255,0), bg='red'):
	"Draw a text on an Image, saves it, show it"
	fnt = ImageFont.truetype('arial.ttf', size)
	# create image
	image = Image.new(mode = "RGB", size = (int(size/2)*len(text),size+50), color = bg)
	draw = ImageDraw.Draw(image)
	# draw text
	draw.text((10,10), text, font=fnt, fill=(255,255,0))
	# save file
	image.save(filename)
	# show file
	os.system(filename)


text_on_img(text="Text to write on img", size=300, bg='red')

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.