Created
January 3, 2021 07:35
-
-
Save arnabsen1729/833df5001e14206d0340925bdd731708 to your computer and use it in GitHub Desktop.
Compression using Gzip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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