Python 3 PyMuPDF Script to Export All Pages of PDF Document as PNG Images Using Pillow Library
48 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-pymupdf-script-to-export-all-pages-of-pdf-document-as-png-images-using-pillow-library/
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 will extract all the images
0:07
from a PDF file it will convert all the
0:11
pages of a PDF document as a image file
0:16
so as you can see this we are taking a
0:18
simple example right here of an PDF
0:22
document it contains nine pages on each
0:26
page you can see there is an image so
0:29
what I will what this script will do it
0:31
will extract all the images it will
0:33
detect all the images on each page let
0:36
me execute the script here you will see
0:38
it will extract all
0:42
the images it will export each page of
0:46
the PDF document as an image file so
0:48
this is the first
0:50
image this is the second one this is you
0:54
can see that
0:57
so this Python script you can run this
1:00
on any PDF and it will extract all the
1:03
images export each page as an image file
1:06
so the link is given in the description
1:09
you can find out the full script let me
1:11
show you step by step how I build this
1:15
so first of all you do need to require
1:17
some packages so the very first package
1:21
you require is the pillow package pie
1:25
l so you do need to install this the
1:30
command is simple then you also need to
1:33
install this package as well py mu
1:38
pdf which is a pdf processing
1:41
package and after that we will just
1:45
require all the necessary packages one
1:47
by one
1:55
so below the package
1:59
here then we specify the image file uh
2:02
the output file that we are working with
2:04
PDF file so we are specifying the path
2:06
here after that we simply open this PDF
2:09
file by using this function open
2:12
function and then we simply running a
2:15
simple for loop for each page in the PDF
2:19
file so this will give the total number
2:22
of pages the length and this for loop
2:25
will run right here and then it will
2:28
extract each
2:31
page so it contains a function here
2:34
which is
2:36
load page so it will load each page by
2:40
the index number after that it will get
2:43
the total number of images which are
2:45
present on each page so this it can
2:48
detect it by using this function which
2:50
is get underscore images and uh here you
2:54
just need to pass this option full to
2:56
true
2:59
so it will get images on the full page
3:02
after that if the images are found then
3:06
in that case it will tell you how many
3:10
images it
3:11
found and you can simply print out this
3:17
message or else there are no images are
3:22
there in this page so we can print out
3:24
the else statement
3:28
and then we simply loop
3:31
through all the images that it has found
3:35
again we'll be running a simple for
3:46
loop so starting it using index one
3:52
so we store the image first
3:55
image and then we extract these images
3:59
using the base 64 code this is the logic
4:02
here so let me
4:05
just paste it this is all that is needed
4:09
after
4:10
that that's all all the source code I've
4:14
given in the description of the video so
4:16
now if you run
4:19
this you will see it is saying on line
4:22
number 16 there is some kind of
4:25
indentation
4:32
error so all the source code is given in
4:35
the description guys so let me just copy
4:38
paste all it just to save some time
4:44
so again if you just run this you will
4:46
see it will extract all these
4:49
images from the PDF and save it inside
4:52
your local machines
4:55
so thank you very much guys for watching
4:57
this video uh please hit that like
4:59
button subscribe the channel and also
5:01
check out my
5:03
website freemediattools.com
5:06
uh which contains thousands of tools