Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2021 07:35
What would you like to do?
Compression using Gzip
import base64
import zlib
def b64encode(data):
encoded = base64.encodebytes(data)
return encoded
def b64decode(data, filename):
decoded = base64.decodebytes(data)
image = open(filename, 'wb')
def gzip64encode(file):
data = b64encode(file)
compressed = zlib.compress(data)
return compressed
def gzip64decode(data):
decompressed = zlib.decompress(data)
b64decode(decompressed, 'gzip_image.png')
if __name__ == "__main__":
image = open(input(), 'rb')
data =
print("IMAGE size: ", len(data))
enc = b64encode(data)
print("B64 encoded: ", len(enc))
b64decode(enc, 'decoded_image.png')
cenc = gzip64encode(data)
print("GZIP-B64 encoded: ", len(cenc))
bcomp = ((len(enc)-len(data))*100/len(data))
gcomp = ((len(enc)-len(cenc))*100/len(enc))
fcomp = ((len(cenc)-len(data))*100/len(data))
print(f"Base64 Expansion: {bcomp:.2f}%")
print(f"Gzip Compression: {gcomp:.2f}%")
print(f"File Inflation: {fcomp:.2f}%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment