提取 EOF 后隐藏的图像

Extract image hidden after EOF

为了学习隐写术,我将图像附加到另一个图像文件,如下所示:

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 格式保存

好的,这就是显示隐藏图像的方法:

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
----------------------------------------