Python 3 pygments Script to Take Screenshot of Source Code and Export to PNG Image
58 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-pygments-script-to-take-screenshot-of-source-code-and-export-to-png-image/
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:05
package which will take the screenshot
0:06
of your source code and export this into
0:09
a image file So let's suppose you have
0:12
some kind of a coding uh source code
0:14
file that you need to share with your
0:16
friends or any sort of blog then you
0:20
just need to take the screenshot So this
0:21
Python script will help in that scenario
0:24
So as you can see we are passing this
0:27
uh this is actually the Python script
0:30
and once I run this Python script we can
0:33
pass any sort of source code It can be
0:35
Python it can be JavaScript it can be C
0:37
C++ So let me run this Python
0:41
script So as soon as I run the Python
0:43
script here you just need to provide the
0:45
path of the source code file that you
0:47
have Let me pass this
0:50
file app 3.Y It's a Python file and then
0:54
just provide the image file which will
0:56
get
0:57
created So now if you see code image
1:00
save
1:01
to so your image has been successfully
1:05
created If I try to open this you will
1:07
see now this is your actual image file
1:10
You can embed this inside any block So
1:13
it contains these line numbers It
1:15
contains the syntax highlighting Your
1:18
Python script has been successfully
1:20
exported to an image
1:22
file and try to open this You will see
1:25
it will look something like this This is
1:27
a simple PNG
1:29
image with syntax highlighting
1:32
everything So there is a package here
1:34
inside Python which you can
1:37
actually use it It's called as pi Gmens
1:42
This is actually the package here It's a
1:44
syntax highlighting package written in
1:46
Python So the command is simple You
1:49
simply install this by executing this
1:52
command So after that you just need
1:54
to you also need one more package which
1:57
will be pillow which will be a image
2:00
processing module So simply install this
2:02
command as well So to install this
2:05
package and after that you just need to
2:08
create a simple Python script So let me
2:12
show you step by step how to get started
2:15
So the first thing you need to do you
2:18
need to import all the packages which
2:20
will be
2:21
necessary like this So from
2:25
this pygm package you need to import the
2:30
highlight
2:36
class So in this way you can import
2:38
everything After
2:41
that you will specify
2:53
this So after this we will simply
2:57
say we will ask the user to provide all
3:00
the arguments here So they will provide
3:03
the source code file and then followed
3:05
by the image file So in this way you can
3:08
use this script So we are getting all
3:10
these two information from the user So
3:13
now we just need to define this custom
3:15
function which will convert this source
3:16
code to a image file So here we are
3:18
specifying this argument So now we just
3:22
need to define this function which will
3:27
be code file to
3:33
image and it will be receiving this uh
3:36
file path and the
3:38
output image path will be none So inside
3:43
this function we will first of all check
3:46
that if the file path is not
3:51
created then we will print out that
3:54
statement that your source code file is
3:58
not existing
4:00
So after that we will open the file that
4:04
you requested to export to image using
4:08
the open function and read the file by
4:11
using this function file do
4:14
read and then inside this try accept
4:18
block we will initialize this uh library
4:22
which will take the
4:26
screenshot and then we We will be using
4:29
the formatter syntax highlighting For
4:31
that we will be initializing this image
4:34
formatter class And inside this it takes
4:37
some options such as which font name you
4:40
want to use for the screenshot if you
4:44
want the line numbers or not Everything
4:46
you can control
4:48
here If you don't want the line numbers
4:51
you can put false here And then you can
4:53
control which image should be created So
4:56
here we are specifying it needs to be a
4:58
PNG image the style everything you can
5:02
control line padding everything So after
5:05
that after you specify these options
5:08
then we can simply create the
5:10
image using the
5:13
highlight class we pass the code we pass
5:16
the lexer and we pass the formatting
5:19
options After that we simply can save
5:22
the image as a PNG
5:25
file by using the open
5:31
function
5:35
So like this after that we can save this
5:39
image by using the save function like
5:42
this
5:47
So I have given the script in the
5:49
description of the video guys If you
5:51
want the full script the link is given
5:53
in the description So once it's
5:55
completed you can actually run the
5:57
script
6:23
So you can see the file will be
6:28
created and let's suppose I change this
6:31
uh options here If you don't want the
6:33
line number let me put here
6:37
false And once again run this you will
6:40
see the line numbers will not be
6:44
there So now you can see the line
6:47
numbers are successfully removed And
6:50
here you can actually get all the styles
6:53
here It basically provides multiple
6:55
themes You can check their
6:57
documentation
7:01
So it has various themes So it basic
7:04
Monokai is a basic theme but you can
7:06
even change that as well You can change
7:08
the padding options Let me change it to
7:10
five And once again run this You will
7:12
see now it will have more
7:16
space like this So the overall script is
7:19
given in the description So thank you
7:21
very much for watching this video and
7:23
also check out my website
7:24
freemediatools.com
7:26
uh which contains thousands of tools
#Clip Art & Animated GIFs
#Multimedia Software
#Scripting Languages