我收到错误too many values to unpack (expected 3)
。当我添加[]
以防它本身是一个元组时,我收到错误not enough values to unpack (expected 3, got 1)
该错误在第 50 行上升,r, g, b = hex2rgb(newpix)
我将分享整个代码,因为我不确定这是否是我遇到的唯一错误。
我的目标是在 .png 文件中插入一个字符串。大约 10 年前我曾经使用过 python,我是 C# 用户,所以请原谅我,如果这作为一个问题太nooby
谢谢 PS我不太会表达自己,请随意编辑帖子
from PIL import Image
import binascii
import optparse
def rgb2hex(r, g, b):
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
def hex2rgb(hexcode):
return tuple(map(ord, hexcode[1:]))
def str2bin(message):
binary = bin(int(binascii.hexlify(message.encode()), 16))
return binary[2:]
def bin2str(binary):
message = binascii.unhexlify('%x' % (int('0b' + binary, 2)))
return message
def encode(hexcode, digit):
if hexcode[-1] in ('0', '1', '2', '3', '4', '5'):
hexcode = hexcode[:-1] + digit
return hexcode
else:
return None
def decode(hexcode):
if hexcode[-1] in ('0', '1'):
return hexcode[-1]
else:
return None
def hide(filename, message):
img = Image.open(filename)
binary = str2bin(message) + '1111111111111110'
if img.mode in ('RGBA'):
img = img.convert('RGBA')
datas = img.getdata()
newData = []
digit = 0
temp = ''
for item in datas:
if (digit < len(binary)):
newpix = encode(rgb2hex(item[0], item[1], item[2]), binary[digit])
if newpix == None:
newData.append(item)
else:
r, g, b = [hex2rgb(newpix)]
newData.append((r, g, b, 255))
digit =+ 1
else:
newData.append(item)
img.putdata(newData)
img.save(filename, "PNG")
return "Completed!"
return "Incorrect Image mode, couldn't hide"
def retr(filename):
img = Image.open(filename)
binary = ''
if img.mode in ('RGBA'):
img = img.convert('RGBA')
datas = img.getdata()
for item in datas:
digit = decode(rgb2hex(item[0], item[1], item[2]))
if digit == None:
pass
else:
binary = binary + digit
if (binary[-16:] == '1111111111111110'):
print("Success")
return bin2str(binary[:-16])
return bin2str(binary)
return "Incorrect Image mode, couldn't retrieve"
def Main():
parser = optparse.OptionParser('usage %prog ' + '-e/-d <target file>')
parser.add_option('-e', dest = 'hide', type = 'string', help = 'target picture path to hide text')
parser.add_option('-d', dest = 'retr', type = 'string', help = 'target picture to retrieve text')
(options, args) = parser.parse_args()
if (options.hide != None):
text = input("Enter a message to hide: ")
print(hide(options.hide, text))
elif(options.retr != None):
print(retr(optrions.retr))
else:
print(parser.usage)
exit(0)
if __name__ == '__main__':
Main()
>>> hex2rgb('#abcdef')
(97, 98, 99, 100, 101, 102)
在这里,我使用您的hex2rgb()
函数而不将输出分配给任何变量。它返回 6 个值,正如您所期望的那样,因为您将十六进制值(a
例如)中的每个字符解码为对应于十六进制a
:的整数 unicode 代码点97
。(请参阅文档或 ord() 此处)
>>> r,g,b = hex2rgb('#abcdef')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
当尝试将它们解包为三个变量时,它会引发错误,因为您的函数返回 6 个变量而不是预期的 3 个。
>>> r,g,b = [hex2rgb('#abcdef')]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 1)
您将 6 个整数包装到一个列表中的方法将返回值从 6 个变量减少到只有 1 个值(具有 6 个项目的列表),因此这不能解决您的问题。
要解决您的问题,您应该找到一种方法来得到 3 个整数而不是 6 个整数。为此,您需要一种不同的方法来解码您的 HEX 值。
我的示例中的 RGB 对由 Red=AB、Green=CD、Blue=EF 组成。通过使用map()
with ord()
you 将A
并B
单独转换为一个整数,这不是您想要的,因为您最终会得到 2 个整数 ( 97
and 98
) 而不是 1 个整数 ( 171
) 对应于该AB
值。
所以你想要的是:
>>> int("0xAB",0)
171
如您所见,AB
一起转换将返回正确的值 (171)。有关如何执行此操作的示例,请参阅此答案。修改您的hex2rgb()
以执行此操作。
最后但并非最不重要的,你的方式来使用ord()
的字符解码成整数很有趣,但可能不正确,因为A
和a
有不同的Unicode代码点值(A = 65
,a = 97
),因此它们导致您的颜色不同的值。在十六进制颜色代码中,大写或小写字母之间没有区别。当您使用上述方法时int()
,小写或大写字符的结果将相同,因此更正确。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句