def compress_each_file_in_folder(): path = './testfiles/' for filename in listdir(path): if '_compressed' not in filename and '_decompressed' not in filename: print(filename) filename, file_extension = os.path.splitext(filename) infile = open(f'{path}{filename}{file_extension}', 'rb') outfile = open(f'{path}{filename}_compressed{file_extension}', 'wb') h = Huffman(infile, outfile) h.compress()
def main(): try: filename = sys.argv[1] except IndexError: print("Задайте файл с помощью аргументов командной строки") return if not os.path.exists(filename): print("Файл не найден!") return print("Исходный файл: '{}' ({} байт)".format(filename, os.path.getsize(filename))) huf = Huffman(filename) codes_filename, res_filename, zeroes = huf.compress(filename) print("Сжатый файл: '{}' ({} байт)".format(res_filename, os.path.getsize(res_filename))) print("Размер таблицы кодов: {} байт".format(sys.getsizeof( huf.codes_table))) print("Нулей дописано в последний байт:", zeroes) dec_filename = huf.decompress(filename, res_filename, zeroes) print("Восстановленный файл: '{}' ({} байт)".format( dec_filename, os.path.getsize(dec_filename)))
def main(): filename = 'ziip.rar' #'test.txt' print("Исходный файл: '{}' ({} байт)".format(filename, os.path.getsize(filename))) huf = Huffman(filename) res_filename, zeroes = huf.compress(filename) print("Сжатый файл: '{}' ({} байт)".format(res_filename, os.path.getsize(res_filename))) print("Нулей дописано в последний байт:", zeroes) dec_filename = huf.decompress(filename, res_filename, zeroes) print("Восстановленный файл: '{}' ({} байт)".format( dec_filename, os.path.getsize(dec_filename)))
komprimerede fil). Begge filer skal åbnes i “binary mode”. Når en BitWriter instantieres, skal den have et file object som argument. Opgave 2: I opgave 2 skal man bruge metoderne readint32bits() og readbit() fra klassen BitReader fra det udleverede bibliotek bitIO.py til at læse heltal (for hyppighedstabel) og bits (for Huffmans-koderne) fra inputfilen (den komprimerede fil). Man skal bruge kaldet write(bytes([b])) (hvor write() er fra file objects og bytes() er en built-in funktion) til skrive bytes til outputfilen (den genskabte originale fil). Her er b et heltal som repræsenterer den byte, som skal skrives. Begge filer skal ˚abnes i “binary mode”. Når en BitReader instantieres, skal den have et file object som argument. :Gruppe medlemmer: Mads Emil Falkenstrøm, [email protected] Mathias Birkebjerg Kristiansen, [email protected] Patrick Nielsen, [email protected] """ import sys from huffman import Huffman if __name__ == '__main__': infile = open(sys.argv[1], 'rb') outfile = open(sys.argv[2], 'wb') h = Huffman(infile, outfile) h.compress()
from huffman import Huffman import sys path = 'images\\sample.bmp' h = Huffman(path) output_path = h.compress() print("Compressed file path: " + output_path) decom_path = h.decompress(output_path) print("Decompressed file path: " + decom_path)
def compress_files(): infile = open('./testfiles/same.txt', 'rb') outfile = open('./testfiles/same_compressed.txt', 'wb') h = Huffman(infile, outfile) h.compress()
from huffman import Huffman pathFile = "./tests/data/textesimple.txt" h = Huffman() h.compressFile(pathFile) compressedData = h.compress("Bonjour!!")
def main(): """ DO NOT change this file """ INPUT_FILE = "FDREconomics.txt" DECRYPT_FILE = "FDREconomicsDecrypt.txt" ENCRYPT_FILE = "FDREconomicsEncrypt.txt" DECRYPT_COMPRESS_FILE = "FDREconomicsDecryptComp.txt" VIGENERE_KEY = "Give PEACE a chance!!" print("(1) Read in original file from " + INPUT_FILE) print(" Print the original file:") print() file_str = open(INPUT_FILE, 'r').read() print(file_str) print() print("(2) Encrypt original file using key: " + VIGENERE_KEY) print() vig = Vigenere(VIGENERE_KEY) en_file_str = vig.encrypt(file_str) print("(3) Write out the encrypted file to " + ENCRYPT_FILE) print() codecs.open(ENCRYPT_FILE, 'w', encoding='utf8').write(en_file_str) print("(4) Read in encrypted original file from " + ENCRYPT_FILE) print(" Decrypt encrypted file using key") print(" Print out decrypted file:") print() en_file_str = codecs.open(ENCRYPT_FILE, 'r', encoding='utf8').read() message = vig.decrypt(en_file_str) print(message) print() print("(5) Write out the decrypted file to " + DECRYPT_FILE) print() open(DECRYPT_FILE, 'w').write(message) print("(6) Compress the original file and save in string") print() huff = Huffman() binary_str = huff.compress(file_str) print("(7) Decompress compressed file from the string") print(" Print out decompressed file") print() message = huff.decompress(binary_str) print(message) print() print("(8) Encrypt original file using key") print() vig = Vigenere(VIGENERE_KEY) en_file_str = vig.encrypt(file_str) print("(9) Compress the encrypted file and save in string") print() huff = Huffman() binary_str = huff.compress(en_file_str) print("(10) Decompress compressed encrypted file from the string") print() message = huff.decompress(binary_str) print("(11) Decrypt decompressed encrypted file using key") print(" Print out decrypted decompressed encrypted file") print() file_str = vig.decrypt(message) print(file_str) print() print("(12) Write out the decrypted decompressed file to " + DECRYPT_COMPRESS_FILE) open(DECRYPT_COMPRESS_FILE, 'w').write(file_str)
def main(): """ """ INPUT_FILE = "FDREconomics.txt" DECRYPT_FILE = "FDREconomicsDecrypt.txt" ENCRYPT_FILE = "FDREconomicsEncrypt.txt" COMPRESS_DAT_FILE = "FDREconomicsComp.dat" ENCRYPT_COMPRESS_DAT_FILE = "FDREconomicsEncryptComp.dat" DECRYPT_COMPRESS_FILE = "FDREconomicsDecryptComp.txt" VIGENERE_KEY = "I love the USA!!" print("(1) Read in original file: Using " + INPUT_FILE) print(" Print the original file:") print() file_str = open(INPUT_FILE, 'r').read() print(file_str) print() print("(2) Encrypt original file using key: '{}'".format(VIGENERE_KEY)) print() vig = Vigenere(VIGENERE_KEY) en_file_str = vig.encrypt(file_str) print("(3) Write out the encrypted file without compression") print(" Encrypted original file: Using " + ENCRYPT_FILE) print() open(ENCRYPT_FILE, 'w').write(en_file_str) print("(4) Read in encrypted original file: " + ENCRYPT_FILE) print(" Decrypt encrypted file using key") print(" Print out decrypted file:") print() en_file_str = open(ENCRYPT_FILE, 'r').read() message = vig.decrypt(en_file_str) print(message) print() print("(5) Write out the decrypted file") print(" Decrypted file: Using " + DECRYPT_FILE) print() open(DECRYPT_FILE, 'w').write(message) print("(6) Compress the original file without encryption") print(" Write out the file compressed without encryption") print(" Compressed original file: Using " + COMPRESS_DAT_FILE) print() huff = Huffman() binary_str = huff.compress(file_str) if BITARRAY_EXISTS: write_bin_file(COMPRESS_DAT_FILE, binary_str) print("(7) Read in compressed original file without encryption") print(" Decompress compressed file") print(" Print out decompressed file") print() if BITARRAY_EXISTS: binary_str = read_bin_file(COMPRESS_DAT_FILE) message = huff.decompress(binary_str) print(message) print() print("(8) Encrypt original file using key") print() vig = Vigenere(VIGENERE_KEY) en_file_str = vig.encrypt(file_str) print("(9) Compress the encrypted file") print(" Write out the compressed encrypted file") print(" Compressed encrypted file: Using " + ENCRYPT_COMPRESS_DAT_FILE) print() huff = Huffman() binary_str = huff.compress(en_file_str) if BITARRAY_EXISTS: write_bin_file(ENCRYPT_COMPRESS_DAT_FILE, binary_str) print("(10) Decompress compressed encrypted file") print(" Read in compressed encrypted file") print(" Compressed encrypted file: Using " + ENCRYPT_COMPRESS_DAT_FILE) print() if BITARRAY_EXISTS: binary_str = read_bin_file(ENCRYPT_COMPRESS_DAT_FILE) message = huff.decompress(binary_str) print() print("(11) Decrypt decompressed file using key") print(" Print out decrypted decompressed file") print() file_str = vig.decrypt(message) print(file_str) print() print("(12) Write out the decrypted decompressed file") print(" Decrypted decompressed file: Using " + DECRYPT_COMPRESS_FILE) open(DECRYPT_COMPRESS_FILE, 'w').write(file_str)
import cv2 import numpy as np import matplotlib.pyplot as plt from skimage import exposure import math from selffunc import * from jpg import JPEGEncode from rel import RLE from huffman import Huffman j = JPEGEncode() j.compress(0.2) j.compress(0.6) j.compress(0.8) rle = RLE() rle.compress() h = Huffman() h.compress(8) h.compress(16) h.compress(32)