Advertisement
dan-masek

Minimal corrections to fix authors code

Apr 8th, 2021
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. from PIL import Image
  2. import numpy as np
  3. from cv2_41 import cv2
  4.  
  5.  
  6. def lz77Compress (image,sw,lab):
  7.     img = cv2.imread(image)
  8.     flat = np.array(img).flatten()
  9.     row = img.shape[0]
  10.     col = img.shape[1]
  11.     ch = img.shape[2]
  12.     tot = row * col * ch
  13.     slidingWindows = sw
  14.     lookAhead = lab
  15.     print("fase 1")
  16.     encodedTuple = []
  17.     encodedChar = []
  18.     # Lunghezza del Search Buffer
  19.     sbSize = slidingWindows - lookAhead
  20.     for it in range(sbSize):
  21.         encodedTuple.append(0)
  22.         encodedTuple.append(0)
  23.         encodedChar.append(flat[it])
  24.     # puntatore Search Buffer
  25.     sbPointer = 0
  26.     print("fase 2")
  27.     while sbPointer < tot :
  28.         max_match = 0
  29.         max_match_go_back = 0        
  30.         selChar = sbPointer + sbSize
  31.      # corrispondenza del carattere in Sb da lookAd
  32.         encodeCharacters = flat[selChar]
  33.     #sequenza vuota[]
  34.        
  35.         seqY = []
  36.         for i in range(sbPointer,sbPointer + sbSize):
  37.             if(flat[i] == encodeCharacters):
  38.                 seqY.append(i)
  39.        
  40.     #controllare se non vi e una corrispondenza
  41.         if(len(seqY) == 0 ):
  42.             encodedTuple.append(0)
  43.             encodedTuple.append(0)
  44.             encodedChar.append (encodeCharacters)
  45.         else:
  46.             for j in seqY:
  47.             #lunghezza della corrispodenza
  48.                 matchLenght= 0
  49.                 returnBack= selChar - j
  50.                 it = 0
  51.                 while selChar + it < tot :
  52.                     if flat[it + j] == flat[selChar + it]:
  53.                         matchLenght +=1
  54.                         it +=1
  55.                   # se non trova corrispondenze
  56.                     else:
  57.                         break
  58.                 if matchLenght>max_match:
  59.                    max_match = matchLenght
  60.                    returnBack= max_match_go_back
  61.  
  62.            
  63.             encodedTuple.append(max_match_go_back)
  64.             encodedTuple.append(max_match)
  65.             encodedChar.append(flat[selChar + max_match - 1])
  66.            
  67.            
  68.  
  69.         sbPointer= sbPointer+ 1 +max_match
  70.    
  71.        
  72.  
  73.    
  74.  
  75.     #print("Prova", encodedTuple, encodedChar)  
  76.     np.save("encodedTuple", encodedTuple)
  77.     np.save("encodedChar", encodedChar)  
  78.     print("File compresso in : Copressed.txt")
  79.     output = open("Compressed.txt","w+")
  80.     imgSize = open('imgSize.txt', "w")
  81.     imgSize.write(str(row) + '\n')  # write row dimension
  82.     imgSize.write(str(col) + '\n')  # write col dimension
  83.     imgSize.write(str(ch) + '\n')  # write col dimension
  84.     output.close()
  85.     imgSize.close()
  86.     cv2.waitKey(0)
  87.     cv2.destroyAllWindows()
  88. # fase di decodifica
  89. # fase di decodifica
  90. def lz77Decompressor():
  91.     imsize = open("imgSize.txt", "r")
  92.     row = int(imsize.readline())
  93.     col = int(imsize.readline())
  94.     ch = int (imsize.readline())
  95.     tot = row * col * ch
  96.  
  97.     # carico Tuple e caratteri
  98.     encodedTuples = np.load("encodedTuple.npy")
  99.     encodedChars = np.load("encodedChar.npy")
  100.     tupleI= 0
  101.     charJ = 0
  102.         #array di decodifica
  103.     decodeArray = []
  104.     while tupleI < encodedTuples.size:
  105.         pBack =  encodedTuples[tupleI]
  106.         charact = encodedChars[charJ]
  107.         #int usato per risolvere error:numoy.int32
  108.         lunghezza = int(encodedTuples[tupleI + 1])
  109.         charJ+=1
  110.         tupleI+=2
  111.         # se non vi e nessun prefisso codificato
  112.         if(pBack == 0):
  113.             decodeArray.append(charact)
  114.         else:
  115.             longT = len(decodeArray)
  116.             for j in range(lunghezza):
  117.                 distance = int(longT - pBack + j)
  118.             if(distance<len(decodeArray)):
  119.                 decodeArray.append(distance)
  120.                 if len(decodeArray) < tot:
  121.                     decodeArray.append( distance)
  122.  
  123.  
  124.     for i in range(len(decodeArray), tot):
  125.         decodeArray.append( i)
  126.     decodeArray = np.reshape(decodeArray,(row,col,ch))
  127.  
  128.    
  129.     cv2.imwrite("output.png", decodeArray)
  130.     print("FinalSize",decodeArray.shape)
  131.     cv2.waitKey(0)
  132.     cv2.destroyAllWindows()
  133.    
  134. path = 'im3.jpg'
  135.  
  136. sliding_window_size = 500
  137. lookahead_buffer_size = 500
  138.  
  139.  
  140. lz77Compress(path, sliding_window_size, lookahead_buffer_size)
  141.  
  142. lz77Decompressor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement