-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageExtractor.py
More file actions
101 lines (84 loc) · 2.96 KB
/
ImageExtractor.py
File metadata and controls
101 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python3
## @file
#
# ImageExtractor.py
#
# Script for extracting images from binary files
#
# Copyright (c) 2025-2026, ilikesn0w. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
##
import os
import sys
from tkinter import Tk
from tkinter.filedialog import askopenfilename
def extract_images_from_file(input_file, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
print(f'The folder {output_folder} has been created.')
with open(input_file, 'rb') as file:
data = file.read()
jpeg_marker = (b'\xFF\xD8', b'\xFF\xD9')
png_marker = (b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A', b'\x49\x45\x4E\x44\xAE\x42\x60\x82')
bmp_marker = (b'BM',)
start_index = 0
image_count = 0
found_images = False
while start_index < len(data):
jpeg_pos = data.find(jpeg_marker[0], start_index)
png_pos = data.find(png_marker[0], start_index)
bmp_pos = data.find(bmp_marker[0], start_index)
positions = [pos for pos in (jpeg_pos, png_pos, bmp_pos) if pos != -1]
if not positions:
break
start_index = min(positions)
if start_index == jpeg_pos:
end_index = data.find(jpeg_marker[1], start_index) + 2
ext = "jpg"
elif start_index == png_pos:
end_index = data.find(png_marker[1], start_index) + len(png_marker[1])
ext = "png"
elif start_index == bmp_pos:
if start_index + 6 > len(data):
break
size = int.from_bytes(data[start_index + 2:start_index + 6], 'little')
end_index = start_index + size
ext = "bmp"
else:
break
if end_index <= start_index or end_index > len(data):
break
image_count += 1
output_file = os.path.join(output_folder, f'image_{image_count}.{ext}')
with open(output_file, 'wb') as img_file:
img_file.write(data[start_index:end_index])
print(f'Image {image_count} saved as {output_file}')
found_images = True
start_index = end_index
if not found_images:
print("No images found.")
Tk().withdraw()
input_file = askopenfilename(title="Select the input file", filetypes=[("All files", "*.*")])
if input_file:
base_name = os.path.splitext(os.path.basename(input_file))[0]
output_folder = f"{base_name}_output_images"
extract_images_from_file(input_file, output_folder)
else:
print("No file selected.")
if sys.platform == "win32":
import msvcrt
print("Press any key to exit...")
msvcrt.getch()
else:
import termios
import tty
print("Press any key to exit...")
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)