Last active
June 17, 2024 02:05
-
-
Save GrayedFox/8cabb5bc81312cbff0a0a9244683d06c to your computer and use it in GitHub Desktop.
Extract JPG image from binary data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# please ensure python means python3 on your system | |
# the file can be any binary file that contains a JPG image | |
# note that it's hungry and doesn't chunk the read so careful with large files | |
# usage: extract-jpg file_name | |
import sys | |
file_name = sys.argv[1] | |
def extract_jpg_image(): | |
jpg_byte_start = b'\xff\xd8' | |
jpg_byte_end = b'\xff\xd9' | |
jpg_image = bytearray() | |
with open(file_name, 'rb') as f: | |
req_data = f.read() | |
start = req_data.find(jpg_byte_start) | |
if start == -1: | |
print('Could not find JPG start of image marker!') | |
return | |
end = req_data.find(jpg_byte_end, start) + len(jpg_byte_end) | |
jpg_image += req_data[start:end] | |
print(f'Size: {end - start} bytes') | |
with open(f'{file_name}-extracted-img.jpg', 'wb') as f: | |
f.write(jpg_image) | |
if __name__ == "__main__": | |
extract_jpg_image() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added back the line I deleted by mistake - I don't mind having the bytearray initialised as empty, I find it makes the snippet easier to read - but feel free to instead just write the JPG chunk of data directly if you prefer 👍