【问题标题】:Add some bit stream to image png向图像 png 添加一些比特流
【发布时间】:2021-06-08 01:23:13
【问题描述】:

我已使用以下程序将 png 图像转换为基本的 2 位序列

from PIL import Image, ImageFile
from io import BytesIO
out = BytesIO()

with Image.open("download.png") as img:
img.save(out, format="png")
image_in_bytes = out.getvalue()
encoded_b2 = "".join([format(n, '08b') for n in image_in_bytes])
w = open("bitofimage", "w")
w.write(encoded_b2)

因此,是一个基数为 2. 的位序列

我创建了一个 CRC 过程并在其后面的位缝中添加了 8 个位。

然后使用此代码转换回 PNG 图像

a = open("bitadd_crc","r") //This Stream of bit has add with 8 bit crc
konv = a.read()
decoded_b2 = [int(konv[i:i + 8], 2) for i in range(0, len(konv), 8)]
with open('YANGDIKIRIM.png', 'wb') as f:
f.write(bytes(decoded_b2))

f.close()
w.close()

再次转换为图像后,它可以工作,但是当我再次将其解压缩为一系列位时,之前添加的 8 位 CRC 无法读取并转换为图像的一部分。谁能帮我找到解决方案? 我希望能帮助我完成最后的学校作业

感谢您的帮助

【问题讨论】:

    标签: python-3.x image-processing type-conversion png bytesio


    【解决方案1】:

    您可以将 CRC 附加到 PNG 文件(作为二进制文件)的末尾,而不是将 CRC 添加到图像数据中。
    读取新文件的应用程序(末尾有少量 for 字节)将忽略冗余字节。

    • 将download.png的内容复制到bitadd_crc.png
    • 将 CRC(作为两个 HEX 数字或其他格式)写入 bitadd_crc.png 文件的末尾。

    bitadd_crc.png 和 download.png 的唯一区别是最后两个字节。


    这是一个代码示例:

    from PIL import Image, ImageFile
    from io import BytesIO
    import crc8
    
    out = BytesIO()
    
    with Image.open("download.png") as img:
        img.save(out, format="png")
        image_in_bytes = out.getvalue()
        encoded_b2 = "".join([format(n, '08b') for n in image_in_bytes])
        #w = open("bitofimage", "w")
        #w.write(encoded_b2)
    
    hash = crc8.crc8()
    hash.update(image_in_bytes)  # Compute CRC using crc8 https://pypi.org/project/crc8/ (just for the example).
    crc = hash.hexdigest().encode('utf8')
    
    # Writing the CRC to bitadd_crc.png
    ################################################################################
    # Copy the content of download.png to bitadd_crc.png and Write the CRC as two HEX digits, to the end of bitadd_crc.png file
    # The only difference of bitadd_crc.png and download.png is going to be the last two bytes
    with open('bitadd_crc.png', 'wb') as f:
        with open('download.png', 'rb') as orig_f:
            f.write(orig_f.read())  # Read the content of download.png and write it to bitadd_crc.png
    
        f.write(crc) # Write the CRC as two HEX digits, to the end of bitadd_crc.png file
    
    
    # Reading the CRC from bitadd_crc.png
    ################################################################################
    with open('bitadd_crc.png', 'rb') as f:
        f.seek(-2, 2)  # # Go to the 2rd byte before the end
        crc_read = f.read(2)  # Read the last two bytes from the binary file.
    
    print('crc = ' + crc.decode('utf8'))
    print('crc_read = ' + crc_read.decode('utf8'))
    

    思考点:
    考虑在解码的 PNG 像素(以字节为单位)上计算 CRC:

    • 将图像转换为 NumPy 数组,如 here 所述。
    • 计算扁平 NumPy 数组的 CRC。
    • 将 CRC 添加为 here 所述的文件元数据。

    在建议的过程中,CRC 仅应用图像的像素(不应用于 PNG 文件的二进制内容)。

    【讨论】:

    • 感谢您的回答,首先我会尝试您的推荐代码并学习它。
    • 我想再问一次。如果 PNG 文件的二进制内容不能按照此代码 geeksforgeeks.org/cyclic-redundancy-check-python 添加 crc 吗? .我让代码将图像转换为二进制流添加 crc 以通过 udp 发送,然后我将再次检查接收器中的 crc 以检查是否存在某些错误。希望你能推荐我
    • 有很多选择。我的建议(和示例)是将文件作为通用二进制文件,并忽略文件是图像的事实。我无法给出完整的诊断,因为您没有发布添加 CRC 的部分
    • stackoverflow.com/questions/67888306/… 。有我在图像中添加 crc 的完整代码。希望你能帮助我。对不起我的英语不好
    • 你能编辑你的问题吗?创建一个我们可以执行和重现问题的代码。我认为您实际上并不需要计算 CRC,您只需附加几个数据字节,例如 b'1234'。然后尝试读取附加的字节。您将看到您无法读取字节,这将是您问题的一个很好且简单的示例。
    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多