""" Warning : to use the program, you should get a B/W (not grayscale !!) image and save it in PBM format (just zeros and ones). With gimp it is one of the export formats offered, and one should chose the "ASCII" format. """

# three next lines to edit depending on your image
size = 50 # the width (and height) of your image.
# For the current version of the program, it should be square

nbsquares = 5 # your image will be divided into nbsquares x nbsquares little squares. Make sure your total size is a multiple of nbsquares

myfile = r'Images/imageAPMEP.pbm'
# just change what is between the ' ' with the path from this program to your image

""" compacts a list of 0 (white) and 1 (black) pixels using RLE encoding, i.e. gives the length of sequences of consecutive pixels (possibly on different lines) of the same color. Starts with white. Parameter : list, return value : string"""
def compact(mylist):
    col=0 # checking whites first
    ct=0 #counter of consecutive pixels of the same color
    result=''
    for i in mylist:
        if i==col:
            ct+=1
        else:
            result+=' '+str(ct)
            ct=1
            col=1-col
    # when the loop ends we still need to write the length
    # of the last sequence
    result+=' '+str(ct)
    return result    

""" next few lines read from a pbm file and creates a list on which I'll work afterwards. The list is cleaned from the three first lines (useless in my case) and from the line breaks"""
lcol=[]

with open(myfile) as entree:
    for i in range(3):
        b = entree.readline()
    while 1:
        byte_s = entree.read(1)
        if not byte_s: # upon reaching EOF
            break
        if (byte_s in ['0','1']):
            lcol.append(int(byte_s))   

# for testing purpose only
#print(len(lcol))
#print(lcol)

#print("lcol",lcol)

""" creating the list to store our smaller squares. Each one will receive the list of pixels of a square shaped zone nbsquares times less wide/high than the original image. """
            
squares=[[[] for i in range(nbsquares)] for j in range(nbsquares)]

sizess = size//nbsquares #size of a small square

""" A line of the big picture is size px long. A small square is sizess px high. So the sizess (height of a small square) x size (lenghth of a line of the big square) first pixels should fill the first line of small squares. The line is thus determined by index//(sizess*size).
The position of a pixel in its line is determined by index%size.
In a line of the big image, the first sizess pixels go to the leftmost square, the sizess next to the second square and so on. We thus determine the square in which a pixel goes with the formula (i%size)//sizess. """


for i in range(size**2):
   squares[i//(sizess*size)][(i%size)//sizess].append(lcol[i])

#last step : calling compact function to get RLE encoding of
# each little square image and printing the result

# list of line names. Won't work if nbsquares>26
ll=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

for line in range(nbsquares):
    for col in range(nbsquares):
        print(ll[line],col+1," : ",compact(squares[line][col]),"\n")

""" On my file engrenage.txt the output looks like :
A 1  :   100 

A 2  :   100 

A 3  :   100 

A 4  :   100 

A 5  :   28 2 6 4 5 5 4 6 3 7 3 7 4 6 4 6 

A 6  :   10 5 5 6 4 7 3 7 3 8 2 8 2 30 

A 7  :   19 1 9 1 8 2 8 2 8 2 7 33 

A 8  :   1 4 5 8 2 80 

A 9  :   30 1 9 2 8 2 8 2 8 2 8 2 8 2 8 

A 10  :   100 

B 1  :   100 

B 2  :   100 

B 3  :   100 

B 4  :   82 5 4 9 

B 5  :   4 6 5 5 5 5 5 5 4 6 3 7 2 8 1 9 1 19 

B 6  :   0 89 1 6 4 

B 7  :   0 80 9 1 10 

B 8  :   0 90 2 8 

B 9  :   0 2 8 1 9 1 9 3 7 4 6 5 5 6 3 31 

B 10  :   60 5 5 6 4 7 3 7 3 

C 1  :   100 

C 2  :   100 

C 3  :   29 1 9 1 9 1 8 2 8 2 8 2 8 2 9 1 

C 4  :   0 100 

C 5  :   0 59 1 8 2 8 2 7 3 7 3 

C 6  :   0 4 6 3 7 2 8 1 69 

C 7  :   100 

C 8  :   3 7 5 5 6 4 7 3 8 2 9 1 9 1 30 

C 9  :   0 80 1 9 1 9 

C 10  :   0 8 2 8 2 8 2 9 1 9 1 9 1 9 1 8 2 7 3 6 4 

D 1  :   100 

D 2  :   100 

D 3  :   9 1 90 

D 4  :   0 10 1 9 4 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5 

D 5  :   0 7 3 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 

D 6  :   100 

D 7  :   100 

D 8  :   100 

D 9  :   1 9 2 8 2 8 2 8 2 8 2 8 2 8 2 8 2 8 2 8 

D 10  :   0 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 4 6 

E 1  :   100 

E 2  :   100 

E 3  :   59 1 9 1 9 1 9 1 9 1 

E 4  :   5 5 5 5 3 7 1 69 

E 5  :   0 6 4 7 3 7 3 7 3 8 2 8 2 9 1 30 

E 6  :   90 2 8 

E 7  :   100 

E 8  :   69 1 8 2 7 3 7 3 

E 9  :   2 8 1 9 1 9 1 69 

E 10  :   0 6 4 8 2 9 1 49 1 9 1 9 1 

F 1  :   100 

F 2  :   82 5 3 7 3 

F 3  :   75 3 6 6 4 6 

F 4  :   0 20 1 9 1 9 2 8 2 7 5 3 13 1 9 2 8 

F 5  :   0 50 2 8 3 7 4 6 5 5 6 4 

F 6  :   0 3 7 4 6 6 4 8 2 60 

F 7  :   40 60 

F 8  :   5 5 4 6 2 78 

F 9  :   0 38 2 7 3 6 4 5 5 4 6 3 7 3 7 

F 10  :   0 8 2 8 2 7 4 5 64 

G 1  :   9 1 9 1 49 1 8 2 2 8 1 9 

G 2  :   0 8 2 77 3 5 5 

G 3  :   4 76 6 4 8 2 

G 4  :   0 2 8 2 8 2 8 2 8 2 8 3 7 4 6 30 

G 5  :   7 3 7 3 7 3 6 4 6 4 6 4 6 5 5 5 6 5 6 2 

G 6  :   0 99 1 

G 7  :   0 51 9 1 39 

G 8  :   0 70 1 9 1 9 2 8 

G 9  :   0 3 7 4 6 4 6 4 6 4 6 4 6 4 6 4 6 2 18 

G 10  :   100 

H 1  :   1 9 1 39 1 9 3 7 4 6 4 6 4 6 

H 2  :   0 4 6 3 7 3 7 2 8 1 9 1 9 1 9 1 9 1 9 1 9 

H 3  :   9 1 90 

H 4  :   0 20 1 9 1 9 2 8 2 7 3 7 3 7 3 7 3 7 1 

H 5  :   0 2 8 2 8 2 8 2 68 

H 6  :   0 9 3 6 82 

H 7  :   100 

H 8  :   3 5 92 

H 9  :   100 

H 10  :   100 

I 1  :   4 6 4 6 3 7 1 9 1 9 1 9 1 9 1 9 2 8 2 8 

I 2  :   0 1 9 1 9 1 9 2 8 2 8 3 7 4 6 5 5 7 3 10 

I 3  :   69 1 8 2 6 14 

I 4  :   2 7 3 8 2 8 1 9 1 54 5 

I 5  :   10 1 9 2 8 3 7 3 7 2 8 2 8 2 8 1 19 

I 6  :   100 

I 7  :   100 

I 8  :   100 

I 9  :   100 

I 10  :   100 

J 1  :   9 1 90 

J 2  :   0 20 1 9 1 9 1 9 1 9 1 8 2 8 3 7 11 

J 3  :   0 60 5 5 5 5 6 4 10 

J 4  :   0 4 6 3 7 3 7 3 7 3 7 3 7 3 7 3 7 1 19 

J 5  :   100 

J 6  :   100 

J 7  :   100 

J 8  :   100 

J 9  :   100 

J 10  :   100 
"""
