[英]Extract image hidden after EOF
For a little lesson in steganography, I am appending an image to another image file like so:对于隐写术的小课,我将图像附加到另一个图像文件,如下所示:
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()
Now I want to extract the hidden image again.现在我想再次提取隐藏的图像。 How would I do this?
我该怎么做? I tried with PIL by reading the image or by reading in the file as a bytes stream and then converting it, but I only get the visible image.
我通过读取图像或将文件读取为字节 stream 然后转换它来尝试使用 PIL,但我只得到可见图像。
In case it matters, I should specify that all images are saved in.jpg format万一这很重要,我应该指定所有图像都保存为 .jpg 格式
Ok got it, this is how to show the hidden image:好的,这是显示隐藏图像的方法:
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()
I was preparing an answer, and just while typing you added your solution.我正在准备一个答案,就在你打字的时候添加了你的解决方案。 Nevertheless, here's my version, capable extracting all images stored in the output image:
尽管如此,这是我的版本,能够提取存储在 output 图像中的所有图像:
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))
Of course, I also used the binary data of the output image, and split the binary data using the JPEG file format structure , the "Start of Image" segment FF D8
to be precise.当然,我也使用了 output 图像的二进制数据,并使用JPEG 文件格式结构拆分二进制数据,准确地说是“图像开始”段
FF D8
。
For the set of images, I used, the output would be the following:对于我使用的图像集,output 如下:
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
----------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.