Python 3 OpenCV Script to Export Text to PNG & JPG Image With Custom Font & Colors Using Numpy
32 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 will show you a Python
0:04
script which uses the Open CV and numpy
0:08
library to actually convert raw text
0:11
into a actual PNG or JPG image so this
0:14
is actually a Python script that I
0:16
written uh so I just provided a random
0:19
text here text with open CV and we can
0:22
even manipulate
0:24
the color of the text the font size as
0:27
well so if I just run this script here
0:30
you will see it will create a image from
0:32
this
0:33
text so image has been saved you will
0:36
see PNG file has been download saved so
0:40
you can just see from raw text we have
0:43
created this image here text with open
0:46
CV with a white background we can
0:48
manipulate this text here we can change
0:50
this text here let's
0:51
suppose hello world this is Python
0:56
so once again it will create this
1:01
image you can just uh adjust all these
1:04
settings the color of the text font size
1:06
everything and uh for this you just need
1:09
to install two modules here so let me
1:13
show you one by one so first of all you
1:15
need to install the open CV
1:18
module so command is simple you install
1:21
this command to install Open CV then we
1:24
also need the second one which is
1:28
numpy so just install these two packages
1:31
and just create a simple app py file and
1:35
then we need to import the packages like
1:41
this after that we define a function uh
1:44
which is text to
1:47
image open cv and here we simply provide
1:50
the text and the output path where the
1:53
image will be stored
1:55
so we have all these options we can
1:58
control font size the thickness of the
2:03
text so right in this now let me just
2:07
turn on
2:13
the GitHub copilot let me turn this off
2:17
so that I can clearly show
2:20
you you go to the settings and just turn
2:24
this
2:27
off so now you will not get these
2:30
annoying suggestions so after this we
2:33
here we
2:35
specify we create this image by numpy
2:38
dot contains this function once and here
2:42
we specify the width and the height of
2:46
the text all these properties we can
2:48
control
2:51
here and then here is the second
2:54
property here which
2:55
controls the color of the text
3:00
in this case we are taking the color to
3:02
be a white background you can change
3:05
this color as well by changing this
3:10
values so after defining this we simply
3:13
provide the phone to be used so open CV
3:16
contains this function which is
3:19
font so quite a number of fonts are
3:21
there you can use any font of your
3:23
choice so let me use the italic font and
3:25
then open CV contains this function cv
3:29
dot put text this is actually the
3:32
function here so here you specify on
3:35
which text the position of the text the
3:40
font
3:42
size each and everything you can here
3:45
specify here and then the position of
3:47
the and the thickness of
3:50
the each option
3:53
here so in this way you can control
3:56
everything and then you can put the
4:00
line A4
4:04
size so after that we simply save the
4:07
image
4:08
by calling this function image write and
4:13
add the location output path and the
4:15
actual image so then we specify the
4:18
image is created successfully
4:24
so that's all this is required and now
4:26
we simply need to call this function so
4:28
for calling this we simply say text to
4:30
image open CV and here we specify your
4:35
text and then we specify the phone
4:39
size which is let's suppose two so
4:45
just run the script and if you see it is
4:48
saying position is not defined i think
4:52
it's a typo mistake so let me correct
5:03
this so if you run this script here you
5:06
will see the image is
5:09
saved so in this easy way guys you can
5:12
create images from raw text inside
5:15
OpenCV and numpy in Python you can
5:18
provide any random text here it will
5:20
create a image of that with white
5:22
background you can change the background
5:24
color font size all these properties so
5:27
you can see
5:28
that so thank you very much for watching
5:31
this video and also check out my website
5:33
freemediatools.com
5:35
uh which contains thousands of tools