Python 3 Pillow Script to Export Text to PNG & JPG Image With Custom Font & Colors
25 views
Jun 3, 2025
Buy the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I'll show you how to use
0:05
the pillow library inside Python to
0:08
actually convert your raw text inside a
0:11
PNG or JPG image so this is actually a
0:13
Python script that I written right here
0:16
and if I just execute this Python script
0:18
you will see a image file will get
0:21
created output.png
0:24
and this is actually converting this uh
0:26
simple text here you will see into an
0:29
image so this text will be written right
0:32
here you can control all these
0:33
properties such as background color the
0:35
color of the text phone size everything
0:38
you can control here you can see we are
0:40
passing 60 as the phone size let me
0:44
change this to 100 and you can even
0:47
change the background color as well this
0:49
is set to red here you can change this
0:53
to yellow and the color of the text can
0:56
change to black so once again if you run
0:58
the script here it will create a brand
1:00
new image with all these modifications
1:03
you will
1:04
see so first of all you just need to
1:08
install this module which is pillow
1:11
pip
1:13
so this is actually the command here
1:15
simply execute this command to install
1:18
this and now I will show you this
1:21
command this script here simply create
1:23
app py
1:26
file so first of all you just need to
1:29
import this module so for importing it
1:31
we simply say
1:36
from pil we need to import the image
1:40
module the image draw module and the
1:43
image font module so in this way you can
1:46
import this module and after that we
1:49
will simply define a custom function so
1:53
here you can define whichever text you
1:55
want
1:56
to create a image from
2:01
so after that we can define a custom
2:04
function which will be responsible for
2:06
converting this text into image so here
2:08
we can pass the
2:10
text and then the second argument will
2:13
be the phone size we can set this to any
2:15
values let's suppose I set to 50 and
2:19
then we define this
2:21
function like this text to
2:24
image so we get the
2:27
text then we also get the output path so
2:30
where you need to create this image and
2:33
then we also have the phone
2:35
size which is set to
2:39
50 and the image size also here you can
2:43
control the size of the image as well so
2:45
let me set it to 800 by
2:49
400 then also we can create just control
2:52
the background color BG color we can set
2:55
this to any color right here and then
2:57
the text color as
2:59
well so as you can see we can control
3:02
all these properties and inside this
3:04
function right here
3:06
we inside try catch block we write the
3:10
code here
3:11
so we we get the font here you can even
3:15
use custom font so using this function
3:18
true type so here you will specify the
3:21
TTF file of the font so AIAL.TF we are
3:25
providing it and then passing the font
3:28
size so here you can even provide your
3:31
custom font as well
3:33
so after that we will load the font so
3:36
it contains this image font dot load
3:42
default so after
3:45
that we need to create the image for
3:48
creating the image we have this image
3:50
dot new function which will create this
3:53
RGB image with the image size and the
3:57
background color
4:00
then we will draw the text on this image
4:02
for drawing the text we have the image
4:04
draw module we call the draw function
4:08
and draw the text and now for drawing
4:11
the text we have the text method so here
4:14
you will specify at which lo which
4:17
location you need to draw the text so I
4:19
will say 10 10 this is the x coordinate
4:21
and the y coordinate the second will be
4:23
the actual text that you want to draw
4:25
and the third argument will be the fill
4:27
color which will be the text color so
4:30
here we are simply providing that
4:32
variable and also here we also need to
4:34
provide the font as well so this will
4:37
draw the text on the image now we simply
4:39
need to save this image on the local
4:41
machine so for saving it we use the save
4:44
method at that output path then we can
4:48
simply write a print statement that
4:50
image
4:51
generated so that's all so this is the
4:54
overall Python script so if I just
4:56
execute this Python script you will see
4:58
on the left hand
5:00
side so this import statement I should
5:04
be yeah so just make this modification I
5:07
should be
5:12
small and also when you put this try
5:16
block you need
5:17
to also have
5:23
the so I've given the script in the
5:26
description of the video
5:31
so so this was yeah so when you put the
5:35
try block you do have to have the accept
5:37
block as well so that was the error so
5:39
if you just execute this you will see
5:41
this output.png PNG file will be
5:44
created so you can see that
5:47
so so in this easy way using this pillow
5:50
library you can create uh images from
5:53
raw text and just control all these
5:55
properties such as image size background
5:57
color and text
5:58
color uh thank you very much for
6:01
watching this video and also check out
6:03
my website freemediatools.com
6:05
uh which contains thousands of tools
#Multimedia Software
#Scripting Languages