如何通过 EXIF / JFIF 签名检测 JPEG 文件?
How can I detect JPEG files by their EXIF / JFIF signature?
几天前,我 最后一个朋友 (@emcconville) 帮我编写了 "Recover every JPEG files in a single file" 的脚本。
现在我意识到这个程序只适用于 "JFIF" 标准的图像,不能检索 "EXIF" 标准的图像(数码相机拍摄的图像)。
如何修改程序使其在图像中也能识别Exif标准?
我不熟悉 Python,也不知道它的威力。
谢谢
import struct
with open('src.bin', 'rb') as f:
# Calculate file size.
f.seek(0, 2)
total_bytes = f.tell()
# Rewind to beging.
f.seek(0)
file_cursor = f.tell()
image_cursor = 0
while file_cursor < total_bytes:
# Can for start of JPEG.
if f.read(1) == b"\xFF":
if f.read(3) == b"\xD8\xFF\xE0":
print("JPEG FOUND!")
# Backup and find the size of the image
f.seek(-8, 1)
payload_size = struct.unpack('<I', f.read(4))[0]
# Write image to disk
d_filename = 'image{0}.jpeg'.format(image_cursor)
with open(d_filename, 'wb') as d:
d.write(f.read(payload_size))
image_cursor += 1
file_cursor = f.tell()
EXIF files have a marker of 0xffe1, JFIF files have a marker of
0xffe0. So all code that relies on 0xffe0 to detect a JPEG file will
miss all EXIF files. (from here)
所以改变一下
if f.read(3) == b"\xD8\xFF\xE0":
到
if f.read(3) == b"\xD8\xFF\xE1":
如果您想检查这两种情况,请不要再那样使用 .read()
。取而代之的是
x = f.read(3)
if x in (b"\xD8\xFF\xE0", b"\xD8\xFF\xE1"):
几天前,我
如何修改程序使其在图像中也能识别Exif标准? 我不熟悉 Python,也不知道它的威力。
谢谢
import struct
with open('src.bin', 'rb') as f:
# Calculate file size.
f.seek(0, 2)
total_bytes = f.tell()
# Rewind to beging.
f.seek(0)
file_cursor = f.tell()
image_cursor = 0
while file_cursor < total_bytes:
# Can for start of JPEG.
if f.read(1) == b"\xFF":
if f.read(3) == b"\xD8\xFF\xE0":
print("JPEG FOUND!")
# Backup and find the size of the image
f.seek(-8, 1)
payload_size = struct.unpack('<I', f.read(4))[0]
# Write image to disk
d_filename = 'image{0}.jpeg'.format(image_cursor)
with open(d_filename, 'wb') as d:
d.write(f.read(payload_size))
image_cursor += 1
file_cursor = f.tell()
EXIF files have a marker of 0xffe1, JFIF files have a marker of 0xffe0. So all code that relies on 0xffe0 to detect a JPEG file will miss all EXIF files. (from here)
所以改变一下
if f.read(3) == b"\xD8\xFF\xE0":
到
if f.read(3) == b"\xD8\xFF\xE1":
如果您想检查这两种情况,请不要再那样使用 .read()
。取而代之的是
x = f.read(3)
if x in (b"\xD8\xFF\xE0", b"\xD8\xFF\xE1"):