Image compression by “def compress(S)” function using run-length encoding

后端 未结 1 1884
攒了一身酷
攒了一身酷 2021-01-07 09:15

I need to write a function called compress(S) that takes a binary string S of length less than or equal to 64 as input and returns another binary s

1条回答
  •  暖寄归人
    2021-01-07 10:03

    First Run Length Encoding, define it as-

    def rle(input_string):
                """ takes input as string and checks repeating bit
                    return repeating bit with their counts.
                """
                count = 1
                prev = ''
                lst = []
                for character in input_string:
                    if character != prev:
                        if prev:
                            entry = (prev,count)
                            lst.append(entry)
                            #print lst
                        count = 1
                        prev = character
                    else:
                        count += 1
                else:
                    entry = (character,count)
                    lst.append(entry)
    
                return lst
    

    Produce list of tuples.

    Input: print rle('1111010')

    Output: [('1', 4), ('0', 1), ('1', 1), ('0', 1)]

     ----------------------------------
    

    Now use this list to make dictionary with binary conversion of repeating counts and format it to 7 bits long. And finally add the respective key and values of the dict so that total digit remain 8 bits.

    def new_dict(S):
                """ input of rle(S) i.e., tuples of bit and repeating counts
                    output dict as bit as key and value as counts with binary conversion.
                """  
    
                dict=rle(S)
                new_dict = []
                temp = []
                for k,v in dict:
                    temp = k + "%07d" % int(bin(v)[2:])
                    new_dict.append(temp)
                return new_dict
    

    input: print new_dict('1111010')

    output: ['10000100', '00000001', '10000001', '00000001']


    Now compress this binary string with the compress(S) function.

            def compress(S):
                """ takes a binary string S of length less than or equal to 64 as input and returns another binary string as output. 
                """
                l = new_dict(S)
                return''.join(str(elem) for elem in l)
    

    print compress('1111010')
    print compress( 64*'0' )

    output: '10000100000000011000000100000001'

    output: '01000000'

    print compress( '11111' )

    output: '10000101'

    Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16

    print compress(Stripes)

    output: '00010000100100000001000010010000'

    0 讨论(0)
提交回复
热议问题