Python 3 Pillow Script to Encode Image to Base64 & Decode Base64 Data to Image in Terminal
404 views
Jun 3, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I'll show you a Python
0:04
script which will actually allows you to
0:07
encode and decode images into base 64
0:10
code so
0:13
uh I have just written this Python
0:15
script here you will see that if I run
0:16
this Python
0:19
script let me just run this Python app
0:23
11 py so now what happens if you see
0:26
image converted to B 64 successfully so
0:30
this is your B 64
0:32
string and you can see that and we can
0:36
in this video I will also show you how
0:38
to convert the B 64 code back into the
0:43
image like this so both the things I
0:46
will show you in this video so this is
0:48
actually the script here it will encode
0:50
and
0:52
decode image to B 64 and B 64 string
0:56
into image so for this purpose we are
0:59
using a package which is a pillow
1:03
package
1:05
so this is the third party package you
1:08
need to
1:09
install it's a image library the command
1:12
is simple i've already installed it this
1:14
pillow package after that you just need
1:17
to require this pillow package and from
1:21
this you need to import the image and
1:24
then we also need to import the base 64
1:28
package and then from the input output
1:30
package we need to
1:34
import the bytes IO
1:37
package after that we will define a
1:40
function which will be responsible for
1:42
encoding the image to base 64 here we'll
1:46
be having the image path which will be
1:48
passed as an
1:51
argument so inside this here we will
1:54
carry out the conversion of the image to
1:57
base 64 code so first of all we will
2:00
open this image using the open
2:05
method like this so opening the image
2:09
here after
2:11
that we open this image using the bytes
2:16
input output after that we save this
2:22
image like this and the format here you
2:25
specify which is image dot
2:29
format and now we just need to convert
2:32
these bytes into p 64 string so for
2:35
converting this we use this function
2:37
which is B 64 dot and now you can see it
2:41
contains various functions so you need
2:43
to use this function
2:45
B64
2:48
encode so this is actually the function
2:50
so here we'll be
2:53
saying we need to pass here
2:57
buffered dot get
3:00
value so we are using this function to
3:03
encode the image into base 64 code and
3:07
then we can simply
3:11
uh
3:14
return the base 64 code that's
3:19
all so this is your function for
3:22
encoding the image to B 64 code now I
3:25
will be defining another function which
3:28
will actually take the base 64 code and
3:31
convert that into an actual image for
3:35
this we use this function which
3:38
is image data and for this we are use
3:43
this function base 64 code dot
3:47
B64 decode instead of encoding we are
3:50
decoding the base 64 code so here we
3:53
will pass the base 64 code
3:59
string after that now we just need to
4:03
create the image from bytes for creating
4:06
the image we use the
4:08
image use the open function and right
4:11
here we say bytes input
4:17
output and we'll pass this image
4:21
data and then we just need to save this
4:23
image so image save and we will save it
4:27
at that uh specified location which we
4:30
have output
4:32
path and then we will simply return a
4:35
boolean parameter return true so your
4:38
image is successfully saved so now we
4:41
successfully define both these functions
4:43
this will be responsible for encoding
4:45
the image to p 64 the second one will be
4:47
responsible for getting the image from B
4:50
64 decoding B 64 code into an actual
4:54
image now we just need to call both
4:56
these functions so for calling this we
5:00
inside our main
5:07
function so right here we will specify
5:10
the input
5:15
path and then we will have the base 64
5:19
string we'll call this function which is
5:22
image
5:24
to base
5:26
64 and pass this input
5:32
path and then we will be getting this
5:35
base 64 code you can print it out and
5:44
then after getting this base 64
5:48
code we can convert back to the image so
5:53
we'll say output path is equal to 1 dot
5:56
jpg and then we will call this
6:00
function base 64 core to image uh
6:05
whatever is your base 64 string plus
6:08
your output
6:11
path that's
6:13
all so if I execute this application
6:20
now so it is saying that you are uh
6:24
expecting the expect or finally block i
6:27
think I missed the finally
6:30
block for this you can remove this try
6:34
block just remove
6:42
it
6:45
uh sorry this needs to be open open
6:49
sorry
6:55
so now you can see it actually encoded
6:58
everything into base 64 code it is
7:00
printed
7:03
out and it created this output dot jpg
7:13
file 1 jpg it has created this because
7:17
if you see we mentioned the output path
7:19
here the file has been already
7:23
created like
7:25
this so in this way guys you can encode
7:28
and decode images from base 64 code
7:31
inside Python so thank you very much for
7:34
watching this video and also check out
7:36
my website freemediatools.com
7:39
uh which contains uh thousands of tools