[英]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()
現在我想再次提取隱藏的圖像。 我該怎么做? 我通過讀取圖像或將文件讀取為字節 stream 然后轉換它來嘗試使用 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()
我正在准備一個答案,就在你打字的時候添加了你的解決方案。 盡管如此,這是我的版本,能夠提取存儲在 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))
當然,我也使用了 output 圖像的二進制數據,並使用JPEG 文件格式結構拆分二進制數據,准確地說是“圖像開始”段FF D8
。
對於我使用的圖像集,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.