0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Base64 to Image File

Use Python's base64 module to decode the base64 string and write the bytes to an image file with open('filename', 'wb'). For example: open('image.png', 'wb').write(base64.b64decode(base64_string)).
📋

Examples

InputiVBORw0KGgoAAAANSUhEUgAAAAUA
OutputA small PNG image file named 'output.png' created from the base64 string.
Input/9j/4AAQSkZJRgABAQEASABIAAD
OutputA JPEG image file named 'output.jpg' created from the base64 string.
Input
OutputNo image file created because the base64 string is empty.
🧠

How to Think About It

To convert base64 to an image, first decode the base64 string back into binary data. Then save this binary data to a file with the correct image extension. This process reverses the base64 encoding that turns image bytes into text.
📐

Algorithm

1
Get the base64 encoded string as input.
2
Decode the base64 string into bytes using a decoding function.
3
Open a new file in binary write mode with the desired image filename.
4
Write the decoded bytes to the file.
5
Close the file to save the image.
💻

Code

python
import base64

base64_string = 'iVBORw0KGgoAAAANSUhEUgAAAAUA'
image_data = base64.b64decode(base64_string)

with open('output.png', 'wb') as file:
    file.write(image_data)

print('Image saved as output.png')
Output
Image saved as output.png
🔍

Dry Run

Let's trace decoding a short base64 string and saving it as an image file.

1

Decode base64 string

Input string 'iVBORw0KGgoAAAANSUhEUgAAAAUA' is decoded to bytes like b'\x89PNG\r\n\x1a\n\x00\x00\x00\r\nIHDR\x00\x00\x00\x05\x00'

2

Write bytes to file

Open 'output.png' in binary write mode and write the decoded bytes.

StepActionValue
1Decode base64b'\x89PNG\r\n\x1a\n\x00\x00\x00\r\nIHDR\x00\x00\x00\x05\x00'
2Write to file'output.png' created with image bytes
💡

Why This Works

Step 1: Decode base64 string

The base64.b64decode() function converts the base64 text back into the original binary image data.

Step 2: Write binary data to file

Opening a file in binary write mode 'wb' allows saving the raw bytes exactly as they are, recreating the image.

🔄

Alternative Approaches

Using io.BytesIO and PIL to load image in memory
python
import base64
from io import BytesIO
from PIL import Image

base64_string = 'iVBORw0KGgoAAAANSUhEUgAAAAUA'
image_data = base64.b64decode(base64_string)
image = Image.open(BytesIO(image_data))
image.save('output_pil.png')
print('Image saved as output_pil.png')
This method lets you manipulate the image in memory before saving but requires the Pillow library.
Using pathlib for file writing
python
import base64
from pathlib import Path

base64_string = 'iVBORw0KGgoAAAANSUhEUgAAAAUA'
image_data = base64.b64decode(base64_string)
Path('output_pathlib.png').write_bytes(image_data)
print('Image saved as output_pathlib.png')
This is a concise way to write bytes to a file using pathlib, improving readability.

Complexity: O(n) time, O(n) space

Time Complexity

Decoding base64 and writing bytes both process each byte once, so time grows linearly with input size.

Space Complexity

The decoded bytes require memory proportional to the image size, so space is linear.

Which Approach is Fastest?

Direct decoding and writing with base64.b64decode and file write is fastest and simplest. Using PIL adds overhead but allows image manipulation.

ApproachTimeSpaceBest For
Direct decode and writeO(n)O(n)Simple conversion to image file
PIL with BytesIOO(n)O(n)Image processing before saving
Pathlib write_bytesO(n)O(n)Concise file writing
💡
Always open the output file in binary write mode 'wb' to correctly save image bytes.
⚠️
Forgetting to decode the base64 string before writing causes corrupted image files.