cornsnake.util_base64

base64 encode/decode utility functions.

Documentation

 1"""
 2base64 encode/decode utility functions.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_base64.html)
 5"""
 6
 7import base64
 8from typing import Any
 9
10
11def encode_to_base64(binary_content: Any, encoding: str = "utf-8") -> str:
12    """Encode binary data (for example that was read from a file in 'rb' mode) into a base64 string."""
13    return base64.b64encode(binary_content).decode(encoding=encoding)
14
15
16def encode_string_to_base64(text: str, encoding: str = "utf-8") -> str:
17    """Encode a string into a base64 string."""
18    byte_data = text.encode(encoding=encoding)
19    return encode_to_base64(binary_content=byte_data)
20
21
22def decode_base64_to_binary(base64_string: str) -> Any:
23    """Decode base64 string to the original binary data."""
24    # Remove the data URL prefix if present
25    if base64_string.startswith("data:"):  # for example: data:image/jpeg;base64,
26        base64_string = base64_string.split(",")[1]
27
28    # Decode the base64 string
29    return base64.b64decode(base64_string)
30
31
32def save_base64_as_binary_file(base64_string: str, output_file_path: str) -> None:
33    """Save base64 string as a decoded binary file."""
34    original_binary = decode_base64_to_binary(base64_string=base64_string)
35
36    # Write the binary data to a file
37    with open(output_file_path, "wb") as file:
38        file.write(original_binary)
39
40    print(f"Image saved successfully as {output_file_path}")
def encode_to_base64(binary_content: Any, encoding: str = 'utf-8') -> str:
12def encode_to_base64(binary_content: Any, encoding: str = "utf-8") -> str:
13    """Encode binary data (for example that was read from a file in 'rb' mode) into a base64 string."""
14    return base64.b64encode(binary_content).decode(encoding=encoding)

Encode binary data (for example that was read from a file in 'rb' mode) into a base64 string.

def encode_string_to_base64(text: str, encoding: str = 'utf-8') -> str:
17def encode_string_to_base64(text: str, encoding: str = "utf-8") -> str:
18    """Encode a string into a base64 string."""
19    byte_data = text.encode(encoding=encoding)
20    return encode_to_base64(binary_content=byte_data)

Encode a string into a base64 string.

def decode_base64_to_binary(base64_string: str) -> Any:
23def decode_base64_to_binary(base64_string: str) -> Any:
24    """Decode base64 string to the original binary data."""
25    # Remove the data URL prefix if present
26    if base64_string.startswith("data:"):  # for example: data:image/jpeg;base64,
27        base64_string = base64_string.split(",")[1]
28
29    # Decode the base64 string
30    return base64.b64decode(base64_string)

Decode base64 string to the original binary data.

def save_base64_as_binary_file(base64_string: str, output_file_path: str) -> None:
33def save_base64_as_binary_file(base64_string: str, output_file_path: str) -> None:
34    """Save base64 string as a decoded binary file."""
35    original_binary = decode_base64_to_binary(base64_string=base64_string)
36
37    # Write the binary data to a file
38    with open(output_file_path, "wb") as file:
39        file.write(original_binary)
40
41    print(f"Image saved successfully as {output_file_path}")

Save base64 string as a decoded binary file.