Python Base64
Python is a high level general purpose programming language which supports several programming paradigms, including object-oriented, functional, structural, imperative, and aspect-oriented. The main architectural features of Python are dynamic typing, automatic memory management, full introspection, exception handling mechanism, multi-thread computing, and convenient high-level data structures. The standard library includes a bundle of useful functions, including the base64
module that allows encoding and decoding data using several Base64 algorithms.
To encode data to Base64 in Python, use the function base64.b64encode:
from base64 import b64encode
print(b64encode('guru')) #-> 'Z3VydQ=='
And of course, the reverse process — to decode Base64 values use the function base64.b64decode:
from base64 import b64decode
print(b64decode('Z3VydQ==')) #-> 'guru'
Everything is quite simple, but if you are looking for some useful examples, check the following ones:
Comments (36)
I hope you enjoy this discussion. In any case, I ask you to join it.
Thank you for kind words. I'm glad you like it.
c$~$2d2|~|dLO!R5(E#C6c3%PgBn8O0fH0{QO8geB~jv`6{*pvNe-upY649F-C%b^B--Ha>}qyBJKovVdVR#+kwZ
import numpy as np
import base64
import math
# 1) Lecture de l’image originale
original_image_path = "/mnt/data/8D4501AC-53F1-4A6C-9B4D-470C096EB761.jpeg"
with open(original_image_path, "rb") as f:
file_data = f.read()
# Décoder l’image en tableau numpy
image_array = np.frombuffer(file_data, np.uint8)
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
# 2) Redimensionnement (largeur max 600 px)
height, width = image.shape[:2]
max_width = 600
if width > max_width:
ratio = max_width / width
new_width = max_width
new_height = int(height * ratio)
image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
# 3) Lissage de la peau (filtre bilatéral + préservation des bords)
# Filtre bilatéral pour réduire le bruit tout en préservant les contours
smooth = cv2.bilateralFilter(image, d=15, sigmaColor=80, sigmaSpace=80)
# Filtre de préservation des bords pour un effet « beauté »
smooth = cv2.edgePreservingFilter(smooth, flags=2, sigma_s=60, sigma_r=0.4)
# 4) Ajustement luminosité/contraste
alpha = 1.2 # Contraste
beta = 20 # Luminosité
bright_contrast = cv2.convertScaleAbs(smooth, alpha=alpha, beta=beta)
# 5) Augmentation de la saturation (légère)
hsv = cv2.cvtColor(bright_contrast, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
# On augmente la saturation de 10%
s = cv2.multiply(s, 1.1)
s = np.clip(s, 0, 255).astype(np.uint8)
hsv_boosted = cv2.merge([h, s, v])
final_image = cv2.cvtColor(hsv_boosted, cv2.COLOR_HSV2BGR)
# 6) Export en JPEG compressé (~70% de qualité)
encode_params = [int(cv2.IMWRITE_JPEG_QUALITY), 70]
_, buffer_jpeg = cv2.imencode(".jpg", final_image, encode_params)
# 7) Encodage en base64
b64_data = base64.b64encode(buffer_jpeg).decode("utf-8")
# Construction de l'URL data
data_url = "data:image/jpeg;base64," + b64_data
# Affichage du résultat (dans le contexte ChatGPT, on va juste print)
print(data_url)