本文介绍了使用运行长度编码的def compress(S)函数的图像压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个名为compress(S)的函数,它将长度小于或等于64的二进制字符串S作为输入,并返回另一个二进制字符串作为输出。输出二进制字符串应该是输入字符串的游程长度编码。



运行长度编码通过8位字节的序列(称为游程长度序列)表示图像:



每个字节的第一位表示下一个将出现在0或1中的位。
最后的7位包含在当前位置连续出现的那些位的二进制数



输出:'01000000'



输出:'10000101'





输出:'00010000100100000001000010010000'



这将非常感谢有人的帮助。 p>

解决方案

首次运行长度编码,定义为 -

  def rle(input_string):
将输入作为字符串并检查重复位
返回重复位及其计数。

count = 1
prev =''
lst = []
用于input_string中的字符:
如果字符! b $ b 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

生成元组列表。



输入: print rle('1111010')



输出:[('1',4),('0',1) '0',1)]

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

现在使用此列表字典,具有重复计数的二进制转换并将其格式化为7位长。最后添加dict的相应键和值,使总数保持8位。

  def new_dict(S):
输入rle(S)即位元和重复计数的元组
输出dict作为关键字,值作为二进制转换的计数


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')



输出:['10000100','00000001','10000001','00000001' p>




现在使用compress(S)函数压缩此二进制字符串。

  def compress(S):
采用长度小于或等于64的二进制字符串S作为输入并返回另一个二进制字符串作为输出

l = new_dict(S)
return''。join(str(elem)for elem in l)



print compress('1111010')

print compress(64 *'0')



输出:'10000100000000011000000100000001'



输出:'01000000'



print compress('11111')



输出:'10000101'



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



print compress(Stripes)



输出:'00010000100100000001000010010000'


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 binary string should be a run-length encoding of the input string.

Run-length coding represents an image by a sequence (called a "run-length sequence") of 8-bit bytes:

The first bit of each byte represents the bit that will appear next in the image, either 0 or 1. The final seven bits contain the number in binary of those bits that appear consecutively at the current location in the image.

Output: '01000000'

Output: '10000101'

Output: '00010000100100000001000010010000'

It will be highly appreciated for someone help.

解决方案

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'

这篇关于使用运行长度编码的def compress(S)函数的图像压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 08:50