Off course there is a way to make the b64 from DALLE smaller by compressing it. You can try the PIL library with Python. CHeck the following example where we can use the PIL library for resizing and compressing it before converting it to base64.

import io
from PIL import Image

# convert base64 image to bytes
image_bytes = io.BytesIO(base64.b64decode(image))

# open image with PIL
pil_image = Image.open(image_bytes)

# this is to resize and compress image
max_size = 256
quality = 80  # adjust quality to get smaller file size
pil_image = pil_image.resize((max_size, max_size))
pil_image.save(image_bytes, format='JPEG', quality=quality)

# convert compressed image to base64
compressed_image = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
Answer from N Miaoulis on Stack Overflow
Top answer
1 of 2
1

Off course there is a way to make the b64 from DALLE smaller by compressing it. You can try the PIL library with Python. CHeck the following example where we can use the PIL library for resizing and compressing it before converting it to base64.

import io
from PIL import Image

# convert base64 image to bytes
image_bytes = io.BytesIO(base64.b64decode(image))

# open image with PIL
pil_image = Image.open(image_bytes)

# this is to resize and compress image
max_size = 256
quality = 80  # adjust quality to get smaller file size
pil_image = pil_image.resize((max_size, max_size))
pil_image.save(image_bytes, format='JPEG', quality=quality)

# convert compressed image to base64
compressed_image = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
2 of 2
0

I'm not sure why N Miaolis's code was not working for my case. I guess saving the new lower quality image in the same BytesIO object did not work for me

I created this method where the image_format is PNG or JPG and the picture.image is a bytes object of the base64 encoded image e.g. b'iVBORw0KGgoAAAA....'

The max_size parameter is used to set the dimensions squared and as for the quality I do not recommend going below 70 as far too few details can be noticed.

def compress_image(picture, quality=70, max_size=256):
    image_bytes = io.BytesIO(base64.b64decode(picture.image))
    image_format = parse_image_format(picture.image_format)

    pil_image = Image.open(image_bytes)
    pil_image = pil_image.resize((max_size, max_size))

    output_buffer = io.BytesIO()
    pil_image.save(output_buffer, format=image_format, quality=quality)
    compressed_image = output_buffer.getvalue()
    picture.image = base64.b64encode(compressed_image)
🌐
Medium
annacsmedeiros.medium.com › efficient-image-processing-in-python-a-straightforward-guide-to-base64-and-numpy-conversions-e9e3aac13312
Efficient Image Processing in Python: A Straightforward Guide to Base64 and Numpy Conversions | by Anna C S Medeiros | Medium
March 30, 2024 - This method can be universally applied to handle images of various file extensions, maintaining the integrity and fidelity of the original data across different image formats. import cv2 import base64 encode_params = [int(cv2.IMWRITE_PNG_COMPRESSION), 9] _, buffer = cv2.imencode('.png', my_opencv_image, encode_params) b64_bytearr = base64.b64encode(buffer).decode("utf-8") b64 = str(b64_bytearr, 'utf-8')
Discussions

python - How do I compress base64 data using stringIO into a buffer? - Stack Overflow
Working on this challenge for my Python course and cant seem to figure out how to continue. I think what i have so far is correct but any input would be amazing. Goal: Decode the base64 blob incl... More on stackoverflow.com
🌐 stackoverflow.com
encode - Compressing and Encoding the contents of a file in python - Stack Overflow
I am trying to compress and encode the contents of a file in python using the zlib.compress() and encode('base64') as follows: More on stackoverflow.com
🌐 stackoverflow.com
August 8, 2013
How to resize base64 encoded image in python - Stack Overflow
I want resize base64 encoded image in python.I searched I could not find. I used Pillow package to do it. However, Pillow has no such kind of feature . More on stackoverflow.com
🌐 stackoverflow.com
Compress GAE Blob images using Python Base64? - Stack Overflow
I'm pulling a JSON request of 20 random thumbnails to an iPhone application. At the moment I'm simply including the image thumb URLs in the JSON array (see below), then the iPhone goes out to get e... More on stackoverflow.com
🌐 stackoverflow.com
November 10, 2011
🌐
Example Code
example-code.com › python › compress_decompress_base64.asp
CkPython Compress and Decompress Base64
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • ...
🌐
GitHub
github.com › cotes2020 › jekyll-theme-chirpy › discussions › 1685
LQIP: Share Python code to compress images and convert them to base64 encoding. · cotes2020/jekyll-theme-chirpy · Discussion #1685
from PIL import Image, ImageFilter ...ge_path,length=16,width=8,radius=2): """ Generate a Low-Quality Image Placeholder (LQIP) and save it to a file, then return the base64 encoded string....
Author   cotes2020
🌐
GitHub
github.com › bigsk1 › Base64-Convert
GitHub - bigsk1/Base64-Convert: Python app to convert and resize images into base64 code and webp - Windows and Linux · GitHub
The Base64 Convert App is a Python script that automates the process of converting images to Base64 format and resizing them. It's particularly useful for embedding images directly into Markdown files for Jekyll-based websites.
Author   bigsk1
🌐
Ask Ubuntu
askubuntu.com › questions › 1349763 › how-to-convert-python-file-from-compress-base64
How to convert python file from compress base64? - Ask Ubuntu
Pasting it into Base64decode.com does offer some accurate-looking strings, but I will not invest any time in cleaning up what could be an incomplete string 🤔 ... I found this coe from some of my friends. And work fine · #!/usr/bin/env python # -*- coding: UTF-8 -*- # collect By Aime_jeux ########################################################################### import marshal,base64,zlib import sys from sys import stdout from uncompyle6.main import decompile import py_compile ########################################################################### def main(): FileSource = '/home/user/D
Find elsewhere
🌐
AskPython
askpython.com › home › encoding an image file with base64 in python
Encoding an Image File With BASE64 in Python - AskPython
April 29, 2023 - When there is large set of images, ... files online. Encoding an image file with Base64 in Python can be done easily using the base64 module....
🌐
GitHub
gist.github.com › makeittotop › 1a2455bbae5c7f4de02e96c78c5ed63c
python-compress-stream-to-gzip-and-b64-encode · GitHub
python-compress-stream-to-gzip-and-b64-encode. GitHub Gist: instantly share code, notes, and snippets.
🌐
CodeWords
codewords.ai › blog › image-convert-base64
Image convert base64: encode and decode in Python | CodeWords
June 3, 2026 - It actually increases size by ~33%. If you need smaller payloads, compress the image first (resize, reduce quality, convert to WebP) and then base64-encode the result.
🌐
Cloudinary
cloudinary.com › home › image conversion to base64 in python: a comprehensive guide
Image Conversion to Base64 in Python: A Comprehensive Guide | Cloudinary
April 24, 2026 - Use Data URI for Small Assets Only Limit the use of Base64 encoding to small assets (under 1 KB) to prevent performance degradation. For larger files, consider traditional loading methods or dynamic on-demand encoding. Automate Conversion for Multiple Images For large projects, create a Python script that automates the conversion of all images in a directory to Base64, saving you from manually handling each file.
🌐
CodingTechRoom
codingtechroom.com › question › -base64-encode-compress-file-python
How to Base64 Encode and Compress a File in Python - CodingTechRoom
This process involves encoding file data in a text format and compressing it to reduce its size. ... import base64 import zlib def encode_and_compress_file(file_path): with open(file_path, 'rb') as file: file_content = file.read() compressed_content = zlib.compress(file_content) base64_encoded ...
🌐
PyPI
pypi.org › project › base64imageCompress
Client Challenge
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
Image to Base64 | Base64 Encode | Base64 Converter | Base64
The Image to Base64 converter generates ready-made examples, depending on the selected output format. It automatically detects the content type of the uploaded image, so that you simply copy the complete result.
🌐
Quora
quora.com › How-do-I-compress-a-text-file-in-Python
How to compress a text file in Python - Quora
Answer (1 of 2): You can use zlib library to compress the binary string of the text file. And to obtain the binary string of the given file, you can use base64 library, and encode the string and pass it to the zlib.compress function which compress it and again decode the information to obtain th...
🌐
Elc
elc.github.io › python-security › chapters › 92_Base64.html
Base64 Encoding — Python Security
Size before compression: 14156 characters Size after compression: 5668 characters Compresion %: 40.04% The process for decoding is straightforward, instead of using a zip file from the filesystem, a bytes object is cast to BytesIO which allows the zipfile module to work with the bytes object as if it were a file. zip_decoded = base64.urlsafe_b64decode(zip_base64_urlsafe) zip_decoded_io = io.BytesIO(zip_decoded) zip_decoded_io.seek(0) zip_handler = zipfile.ZipFile(zip_decoded_io, "r") decoded_unzipped_bytes = zip_handler.read(file_path.name) if decoded_unzipped_bytes == file_path.read_bytes(): print("The extracted file is the same") else: print("The extracted file is NOT the same")
🌐
Base64 Encode
base64encode.net › encode › b1WH
Base64 Encode: compressor
Image to Base64 · File to Base64 · PDF to Base64 · Base32 Encode · PHP base64_encode() Python base64.b64encode() Perl encode_base64() VB System.Convert.ToBase64String() C# System.Convert.ToBase64String() Java encode() Golang EncodeToString() Ruby Base64.encode64() MySQL to_base64() Dojo dojox.encoding.base64.encode() PostgreSQL encode() Linux base64 encode ·