Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用提取的比特重新创建图像并保存在txt文件中_Python_Image_Byte - Fatal编程技术网

Python 使用提取的比特重新创建图像并保存在txt文件中

Python 使用提取的比特重新创建图像并保存在txt文件中,python,image,byte,Python,Image,Byte,我试图从图像中提取二进制信息(如\xff),并将其保存到文本文件中。最终,我还应该能够反转效果,并从文本文件生成图像 我正在使用下面的代码尝试创建文本文件;它不会抛出错误,但生成的文本不会生成图像 您应该在open()函数上使用“rb”和“wb”属性以二进制模式读/写文件: 试试这个: with open(image1, "rb") as input_file: data = input_file.read() with open(image2, "wb&

我试图从图像中提取二进制信息(如
\xff
),并将其保存到文本文件中。最终,我还应该能够反转效果,并从文本文件生成图像

我正在使用下面的代码尝试创建文本文件;它不会抛出错误,但生成的文本不会生成图像


您应该在open()函数上使用“rb”和“wb”属性以二进制模式读/写文件:

试试这个:

with open(image1, "rb") as input_file:
    data = input_file.read()

with open(image2, "wb") as output_file:
    output_file.write(data)
import numpy as np

Bytes = np.fromfile("image1.png", dtype="uint8")
Bits = np.unpackbits(Bytes)


with open("bits.txt", "w") as export_bits:
    export_bits.write("".join(list(map(str, Bits))))

with open("bits.txt", "r") as load_bits:
    string_bits_data = list(map(int, load_bits.read()))


save_new_image = np.packbits(string_bits_data).astype('int8').tofile("image2.png")
print("Done")
但是,如果您想将目标文件字节转换为位(01),并保存它们,然后再次反转此作业,您只需使用numpy软件包:

试试这个:

with open(image1, "rb") as input_file:
    data = input_file.read()

with open(image2, "wb") as output_file:
    output_file.write(data)
import numpy as np

Bytes = np.fromfile("image1.png", dtype="uint8")
Bits = np.unpackbits(Bytes)


with open("bits.txt", "w") as export_bits:
    export_bits.write("".join(list(map(str, Bits))))

with open("bits.txt", "r") as load_bits:
    string_bits_data = list(map(int, load_bits.read()))


save_new_image = np.packbits(string_bits_data).astype('int8').tofile("image2.png")
print("Done")

mikhail-v(UID:4157407)的信用证来自:。

您尝试过什么?失败的原因是什么?你说的“比特”是什么意思?对不起,我忘记了file.write(str(data))而不是file.write(data),我指的是比特,例如“\xff”@bob bytes?位为1/8字节