Here is a program that I've wrote to extract JPEGs from a file. It reads a file that contains the image data and separates it into individual images.
import hashlib
inputfile = 'data.txt'
marker = chr(0xFF)+chr(0xD8)
# Input data
imagedump = file(inputfile, "rb").read()
imagedump = imagedump.split(marker)
count=0
for photo in imagedump:
name = hashlib.sha256(photo).hexdigest()[0:16]+".jpg"
file(name, "wb").write(marker+photo)
count=count+1
print count
The script names the identified images with their SHA256 digest, and all of the photos that it finds will be dumped into the current directory.
Here's how I test the script to see if it is working correctly:
- Type
cd ~/images/
- create the folder
mkdir test
- dump some JPEGs into a singe file in the directory
cat *.jpg > ./test/data.txt
cd test
and put the script into the current directory- run the script
python extract.py
, and the JPEGs will be dumped into the current folder
How can I improve my script's performance? Are there any problems with its operation? The script does not know when an image ends; just when a new one starts. Could this cause problems?