In: Computer Science
compress(image)
Description:
It compresses a grayscale image and returns a data structure (list of lists) that holds a summary of the content, based on the following algorithm: First, we construct from the original image a black and white bitmap – values less than 128 are mapped to black and the rest are mapped to white. Then, we encode each row of this bitmap as a list containing the lengths of the alternating black and white sequences. Last, we package all these lists in an outer list. You can think of the encoding of each row as the process of counting the length of the consecutive black and white pixels (starting always with the black pixels) and adding these values in a list in the same order as we encounter the alternating black and white sequences. Obviously, each row, depending on its content, will have a different number of alternating sequences, so the full data structure will most probably be a list of variable-size sublists.
Parameters: image (2D list of int) is the grayscale image
Return value: A list of lists that summarizes the image
Example:
compress([[100,170,200,111,250,128],
[223,244,255,115,127,105],
[100,100,100,120,120,120]]) → [ [1, 2, 1, 2], [0, 3, 3], [6] ]
Hi. I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
''' required method '''
def compress(image):
    #creating a list to store the result
    summary=[]
    #looping through each row
    for row in image:
        #initializing count to 0
        count=0
        #currently processing black pixels
        black=True
        #list to store the consecutive black, white pixels in current row
        current_summary=[]
        # looping through each column in current row
        for col in row:
            #if col<128, it is counted as black, else white
            if col<128:
                #checking if we are already processing black
                if black:
                    #simply incrementing count
                    count+=1
                else:
                    #otherwise append current count to current_summary
                    current_summary.append(count)
                    #resetting count to 1
                    count=1
                    #setting black to True
                    black=True
            else: #white
                #if we are already processing white, incrementing count
                if not black:
                    count+=1
                else:
                    #otherwise appending count to current_summary
                    current_summary.append(count)
                    #updating count
                    count=1
                    #setting black to False
                    black=False
        #at the end, if count is above 0, appending to current_summary
        if count>0:
            current_summary.append(count)
        #appending current_summary to summary list
        summary.append(current_summary)
    #returning the list
    return summary