Skip to content

Instantly share code, notes, and snippets.

@arnabsen1729
Created January 3, 2021 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnabsen1729/833df5001e14206d0340925bdd731708 to your computer and use it in GitHub Desktop.
Save arnabsen1729/833df5001e14206d0340925bdd731708 to your computer and use it in GitHub Desktop.
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')
image.write(decoded)
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 = image.read()
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))
gzip64decode(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