Image Compression By "def Compress(s)" Function Using Run-length Encoding
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 string as output. The output binar
Solution 1:
First Run Length Encoding, define it as-
defrle(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 += 1else:
                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.
defnew_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 indict:
                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.
defcompress(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)
Post a Comment for "Image Compression By "def Compress(s)" Function Using Run-length Encoding"