-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlrle.py
76 lines (64 loc) · 2.25 KB
/
lrle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Run-Length Encoding
import numpy as np
import imageio
from matplotlib import pyplot as plt
# Function to encode image with RLE
def encode(image):
encoded_string = ''
(rows, cols) = image.shape
for i in range(rows):
encoded_row = ''
# Keep the first pixel as the previous pixel
previous_pixel = image[i, 0]
count = 1
# Start loop from the next pixel
for j in range(1, cols):
current_pixel = image[i, j]
if previous_pixel == current_pixel:
count += 1
else:
# If current pixel is different, encode the previous pixel
# with count. Set the current pixel as next previous pixel
# and reset count to 1
string = str(previous_pixel) + '@' + str(count) + ' '
encoded_row = encoded_row + string
previous_pixel = current_pixel
count = 1
# If it is the last pixel, encode it
if j == cols - 1:
string = str(previous_pixel) + '@' + str(count) + ' '
encoded_row = encoded_row + string
# Append new line to the encoded string
encoded_string += encoded_row + '\n'
return encoded_string
# Function to decode the RLE encoded image from encoded string
def decode(string):
encoded_rows = string.split('\n')
# Number of rows = Number of lines in the encoded string
rows = len(encoded_rows)
# Number of columns = Sum of all the counts in one row
encoded_row = encoded_rows[0].split()
count = 0
for encoded in encoded_row:
print(encoded)
count += int(encoded.split('@')[1])
cols = count
image = np.zeros((rows, cols))
for i, encoded_row in enumerate(encoded_rows):
encoded_row = encoded_row.split()
j = 0
for string in encoded_row:
pixel = int(string.split('@')[0])
count = int(string.split('@')[1])
for k in range(count):
image[i, j] = pixel
j += 1
return image
image = imageio.imread('les_binary.jpg')
# Encode the image
encoded = encode(image)
print(encoded)
# Decode the image
decoded_image = decode(encoded)
plt.imshow(decoded_image, cmap='gray')
plt.show()