提取EOF后隐藏的图像

2024-05-16 12:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

对于隐写术的一个小教训,我将图像附加到另一个图像文件中,如下所示:

my_image = open(output_image_path, "wb")
my_image.write(open(visible_image, "rb").read())
my_image.write(open(hidden_image, "rb").read())
my_image.close()

现在我想再次提取隐藏图像。我该怎么做?我尝试通过读取图像或将文件作为字节流读取,然后转换它来使用PIL,但我只得到了可见的图像

如果有必要,我应该指定所有图像都以.jpg格式保存


Tags: path图像imageclosereadoutputmy图像文件
2条回答

好了,这是如何显示隐藏图像:

from io import BytesIO
import cv2
from PIL import Image

with open(my_image, 'rb') as img_bin:
    buff = BytesIO()
    buff.write(img_bin.read())
    buff.seek(0)
    bytesarray = buff.read()
    img = bytesarray.split(b"\xff\xd9")[1] + b"\xff\xd9"
    img_out = BytesIO()
    img_out.write(img)
    img = Image.open(img_out)
    img.show()

我正在准备答案,就在键入时,您添加了解决方案。不过,这是我的版本,能够提取输出图像中存储的所有图像:

from io import BytesIO
from PIL import Image

# Create "image to the world"
my_image = open('to_the_world.jpg', 'wb')
my_image.write(open('images/0.jpg', 'rb').read())   # size=640x427
my_image.write(open('images/1.jpg', 'rb').read())   # size=1920x1080
my_image.write(open('images/2.jpg', 'rb').read())   # size=1920x1200
my_image.close()

# Try to read "image to the world" via Pillow
image = Image.open('to_the_world.jpg')
print('Read image via Pillow:\n{}\n'.format(image))

# Read "image to the world" via binary data
image = open('to_the_world.jpg', 'rb').read()

# Look for JPG "Start Of Image" segments, and split byte blocks
images = image.split(b'\xff\xd8')[1:]

# Convert byte blocks to Pillow Image objects
images = [Image.open(BytesIO(b'\xff\xd8' + image)) for image in images]
for i, image in enumerate(images):
    print('Extracted image #{}:\n{}\n'.format(i+1, image))

当然,我也使用了输出图像的二进制数据,并使用JPEG file format structure分割二进制数据,确切地说是“图像的开始”段FF D8

对于我使用的图像集,输出如下:

Read image via Pillow:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x427 at 0x1ECC333FF40>

Extracted image #1:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x427 at 0x1ECC333FF10>

Extracted image #2:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1920x1080 at 0x1ECC37D4C70>

Extracted image #3:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1920x1200 at 0x1ECC37D4D30>
                    
System information
                    
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1.1
Pillow:        8.2.0
                    

相关问题 更多 >