if I insert an image of size (205,205) the algorithm compresses it very quickly. But if I insert a larger image, the algorithm takes a very long time to compress it. my intention would be to optimize the code and consequently speed up the compression phase do you have any suggestions?
library
from PIL import Image
import numpy as np
from cv2 import cv2
**function of compression**
def lz77Compress (image,sw,lab):
img = cv2.imread(image)
flat = np.array(img).flatten()
row = img.shape[0]
col = img.shape[1]
ch = img.shape[2]
tot = row * col * ch
slidingWindows = sw
lookAhead = lab
array of tuple and char
encodedTuple = np.array([], dtype=np.uint16)
encodedChar = np.array([], dtype=np.uint8)
**# Lunghezza del Search Buffer**
sbSize = slidingWindows - lookAhead
for it in range(sbSize):
encodedTuple = np.append(encodedTuple, (0, 0))
encodedChar = np.append(encodedChar, flat[it])
**# pointer in the Search Buffer**
sbPointer = 0
while sbPointer < tot :
max_match = 0
max_match_go_back = 0
selChar = sbPointer + sbSize
# corrispondenza del carattere in Sb da lookAd
encodeCharacters = flat[selChar]
**#sequenza vuota[]**
seqY = np.array([],dtype=np.int16)
for i in range(sbPointer,sbPointer + sbSize):
if(flat[i] == encodeCharacters):
seqY = np.append(seqY,i)
**check of corrispondence and insert in encodedtuple and encodedChar**
if(seqY.size == 0 ):
encodedTuple = np.append(encodedTuple,(0,0))
encodedChar = np.append (encodedChar,encodeCharacters)
else:
for j in seqY:
**size of corrisponddence**
matchLenght= 0
returnBack= selChar - j
it = 0
while selChar + it < tot :
if flat[it + j] == flat[selChar + it]:
matchLenght +=1
it +=1
**# if there is not corrispondence*
else:
break
if matchLenght>max_match:
max_match = matchLenght
returnBack= max_match_go_back
encodedTuple = np.append(encodedTuple,(max_match_go_back,max_match))
encodedChar = np.append(encodedChar,flat[selChar + max_match - 1])
sbPointer+= 1 +max_match
**save encoedTupe and encodedChar and image size for the compression**
np.save("encodedTuple", encodedTuple)
np.save("encodedChar", encodedChar)
print("File compresso in : Copressed.txt")
output = open("Compressed.txt","w+")
output.write(str(c))