【问题标题】:Read bytes of a PNG in python在python中读取PNG的字节
【发布时间】:2018-12-13 06:25:06
【问题描述】:

我想读取 PNG 的字节,并修改它们。我已经尝试过这段代码:

import codecs
ed = input("Would you like to encode or decode?\n(e/d)> ")
image = input("What image would you like to use?\n> ")
if(ed == 'e'):
    imagef = codecs.open(image, encoding = "hex")
    imagel = imagef.read()
    img = imagel.decode('hex')
    print(img)

但我收到以下错误:

Would you like to encode or decode?
(e/d)> e
What image would you like to use?
> i.png
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    imagel = imagef.read()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 700, in read
    return self.reader.read(size)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 503, in read
    newchars, decodedbytes = self.decode(data, self.errors)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/hex_codec.py", line 25, in decode
    return hex_decode(input, errors)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/hex_codec.py", line 19, in hex_decode
    return (binascii.a2b_hex(input), len(input))

binascii.Error: Non-hexadecimal digit found

我该如何解决这个问题?

【问题讨论】:

  • 十六进制不是二进制的。您需要以二进制模式读取图像。不要使用codecs
  • @Jean-François Fabre 我不知道如何以二进制模式读取文件。在没有编解码器(使用 open)的情况下读取它时,出现此错误:Traceback (most recent call last): File "main.py", line 7, in &lt;module&gt; imagel = imagef.read() File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
  • 但现在你又再次使用解码器了。 (Python 的默认解码器,旨在读取 UTF8 文本。)PNG 不是任何形式、方式或含义的“编码文本”。阅读有关阅读 bytes 的信息。然后 - only 然后,如果您可以成功读取这些字节 - 请阅读 PNG 规范。请特别注意解释 Flate 压缩的部分,即使对于比您更有经验的程序员来说也很难。然后,放弃并使用库。

标签: python python-3.x hex png


【解决方案1】:

退一步 - 你的假设不正确

png 文件没有“十六进制内容”——它们有字节——“十六进制”不是你想的那样,它只是表示数字的另一种方式。

您的代码无法修复,因为它首先没有任何意义 - 没有什么可解码的。

程序员使用十六进制是为了方便,因为每个十六进制数字 可以表示 4 位,因此只需要两个十六进制数字来表示一个字节。然而,这只是代表。

编辑:

从 cmets 看来,您想要的是将二进制数据添加到文件末尾:

message = 'Hello World!'

with open(image, 'ab') as f: # open the file for appending, binary mode
    f.write(bytes(8))  # write 8 null bytes to the file
    f.write(message.encode('utf-8'))  # add the message, encoded to bytes
                                      # by using the utf-8 encoding

然后再次阅读消息:

with open(image, 'rb') as f: # open the file in read binary mode
    data = f.read() # read the bytes from the file to a variable

pos = data.find(bytes(8)) # locate the 8 null bytes
message = data[pos + 8:].decode('utf-8')  # decode the message

【讨论】:

  • 对不起,我不知道。有没有办法在 python 中读取/修改 PNG 的字节?
  • @JohnSmith 确定!但是怎么做?为什么?你要修改什么?
  • 我的任务是在图像中隐藏消息。我的想法是在末尾添加八个空字节,然后用十六进制编码消息 - 或者字节,我猜。然后编写一个程序来查找八个空字节并从那里开始解码消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2020-01-23
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
相关资源
最近更新 更多