Skip to content

Instantly share code, notes, and snippets.

@cjnghn
Last active September 3, 2023 06:21
Show Gist options
  • Save cjnghn/726f279c0a2500b1339c589c3a519226 to your computer and use it in GitHub Desktop.
Save cjnghn/726f279c0a2500b1339c589c3a519226 to your computer and use it in GitHub Desktop.
base64 vs byte
import os
import base64
def compare_file_and_base64_size(file_path):
try:
with open(file_path, "rb") as file:
original_bytes = file.read()
# Encode the bytes to Base64
base64_encoded = base64.b64encode(original_bytes)
# Calculate sizes
original_size = os.path.getsize(file_path)
base64_size = len(base64_encoded)
return original_size, base64_size
except FileNotFoundError:
return None
result = compare_file_and_base64_size('./Sans_undertale.jpg')
if result:
original_size, base64_size = result
print(f"Original Size: {original_size} bytes")
print(f"Base64 Encoded Size: {base64_size} bytes")
print(f"\n{base64_size / original_size * 100 - 100}%")
else:
print("File not found.")
"""
Original Size: 16942 bytes
Base64 Encoded Size: 22592 bytes
33.34907330893637%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment