"def compress(S)" 函数使用 运行 长度编码进行图像压缩
Image compression by "def compress(S)" function using run-length encoding
我需要编写一个名为 compress(S)
的函数,它将长度小于或等于 64 的二进制字符串 S
作为输入,将另一个二进制字符串 returns 作为输出。输出二进制字符串应该是输入字符串的 运行 长度编码。
运行-长度编码用8位字节的序列(称为"run-length sequence")表示一幅图像:
每个字节的第一位代表图像中下一个将出现的位,0 或 1。
最后七位包含在图像中当前位置连续出现的那些位的二进制数。
>>>compress( 64*'0' )
'01000000'
>>> compress( '11111' )
'10000101'
>>> Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16
>>> compress(Stripes)
'00010000100100000001000010010000'
首先运行长度编码,定义为-
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
生成元组列表。
输入:print rle('1111010')
输出:[('1', 4), ('0', 1), ('1', 1), ('0', 1)]
----------------------------------
现在使用这个列表来制作字典,将重复计数进行二进制转换,并将其格式化为 7 位长。最后添加字典的相应键和值,使总数字保持 8 位。
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
输入:打印new_dict('1111010')
输出:['10000100', '00000001', '10000001', '00000001']
现在用 compress(S) 函数压缩这个二进制字符串。
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)
打印压缩('1111010')
打印压缩( 64*'0' )
输出:'10000100000000011000000100000001'
输出:'01000000'
打印压缩('11111')
输出:'10000101'
条纹 = '0'*16 + '1'*16 + '0'*16 + '1'*16
打印压缩(条纹)
输出:'00010000100100000001000010010000'
我需要编写一个名为 compress(S)
的函数,它将长度小于或等于 64 的二进制字符串 S
作为输入,将另一个二进制字符串 returns 作为输出。输出二进制字符串应该是输入字符串的 运行 长度编码。
运行-长度编码用8位字节的序列(称为"run-length sequence")表示一幅图像:
每个字节的第一位代表图像中下一个将出现的位,0 或 1。 最后七位包含在图像中当前位置连续出现的那些位的二进制数。
>>>compress( 64*'0' )
'01000000'
>>> compress( '11111' )
'10000101'
>>> Stripes = '0'*16 + '1'*16 + '0'*16 + '1'*16
>>> compress(Stripes)
'00010000100100000001000010010000'
首先运行长度编码,定义为-
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
生成元组列表。
输入:print rle('1111010')
输出:[('1', 4), ('0', 1), ('1', 1), ('0', 1)]
----------------------------------
现在使用这个列表来制作字典,将重复计数进行二进制转换,并将其格式化为 7 位长。最后添加字典的相应键和值,使总数字保持 8 位。
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
输入:打印new_dict('1111010')
输出:['10000100', '00000001', '10000001', '00000001']
现在用 compress(S) 函数压缩这个二进制字符串。
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)
打印压缩('1111010')
打印压缩( 64*'0' )
输出:'10000100000000011000000100000001'
输出:'01000000'
打印压缩('11111')
输出:'10000101'
条纹 = '0'*16 + '1'*16 + '0'*16 + '1'*16
打印压缩(条纹)