Efficient Image Processing in Python: A Straightforward Guide to Base64 and Numpy Conversions

Welcome! Here, we will explore some very useful image processing techniques in Python.

Anna C S Medeiros
3 min readMar 23, 2024
  • Disk to Base64 Encoding: Learn the method to read images stored on disk and encode them into base64format ensuring no data loss, leveraging Python’s built-in libraries.
  • Numpy.ndarray to Base64 Encoding: Explore how to transform images from numpy.ndarray format to base64, facilitating web transmission without compromising quality.
  • Base64 to Numpy.ndarray Decoding: Understand the reverse process of converting base64 encoded images back into numpy.ndarray, crucial for image manipulation and processing in Python.
  • Measuring Image Size in Memory: Acquire skills to ascertain the memory size of image objects, an essential aspect of optimizing storage and processing efficiency.

Understanding Image Compression

When saving images, the choice between using lossy or lossless compression will depend on the specific use case:

  • Lossy Compression: Employed for scenarios where reducing file size takes precedence over image fidelity. Predominantly used in bandwidth-sensitive applications like web and video streaming, where it’s imperative to minimize data transfer volume at the expense of losing some image detail.
  • Lossless Compression: Recommended when the integrity of the original image is paramount. Essential in fields like Computer Vision and Machine Learning, where accurate data representation is critical, ensuring no alteration in image details during compression.

This guide aims to equip you with the technical skills to handle image data, emphasizing Lossless Compression and accurate data representation in Python.

Encoding Images to Base64 Without Loss

import base64

with open("image.jpg", "rb") as image_file:
buffer = image_file.read()
img_base64 = base64.b64encode(buffer).decode("utf-8")

In this context, the variable img_base64 will be encoded utilizing the identical codec corresponding to the file extension of the source image.

Specifically, for a JPG file, the base64 encoding process preserves the original image's content in its entirety, ensuring a lossless conversion due to the absence of compression.

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.

Encoding a Numpy.ndarray Image to Base64

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')

In the code abovemy_opencv_image refers to an image loaded via OpenCV, represented as a numpy.ndarray.

To preserve the original characteristics of the image, I recommend using the PNG format with compression levels ranging from 1 (faster and less compression) to 9 (slower and more compression).

This format is selected due to its lossless compression capability, ensuring no degradation of the original my_opencv_image quality.

Additional configuration options can be adjusted using various flags, details of which can be found at the specified link.

Caution: Usingencode_params=[int(cv2.IMWRITE_JPEG_QUALITY), 100]would result in loss, and the original image could not be restored. Even using 100 as the compression parameter, loss is inevitable. Therefore, we do not recommend this approach.

Decoding Base64 back to Numpy.ndarray

import numpy as np
import base64
import cv2

decoded = base64.b64decode(b64)
nparr = np.frombuffer(decoded, np.uint8)
bgr_img = cv2.imdecode(nparr, flags=cv2.IMREAD_COLOR)

Here, b64 is an image in base64format, andbgr_img is a 3-channel image in BGR format.

More flags can be found at this link.

Measuring Image Size in Memory

Checking the memory size of an object is very simple and useful when testing if there was any change to the image.

from sys import getsizeof
print(f'{getsizeof(img_base64) * 0.001} kB')
print(f'{getsizeof(np_array) * 0.001} kB')

. . .

Thanks for reading!

Hope I helped you effectively manage image decoding and encoding tasks, ensuring data integrity and optimizing performance!

Give it some claps to make others find it too! Make sure you follow me on Medium to not miss anything. Also, let’s be friends on LinkedIn.

--

--

Anna C S Medeiros
Anna C S Medeiros

Written by Anna C S Medeiros

Senior Data Scientist @ Vsoft | GenAI | Computer Vision | NLP | LLM

Responses (2)