nick has a blog

Stripping JFIF Comments from JPEG Images

without comments

Most JPEG images include a JFIF comment, which usually describes the creator of the image (not to be confused with Exif data). This data is usually in the range of 50 to 100 bytes, so not much, but it might make a difference if you have a 56k modem and a lot of images. Because this data is effectively useless it is safe to remove it from images.

Here is a python script that will do it:

from binascii import b2a_hex
import sys
 
img = open(sys.argv[1], 'rb')
 
result = ""
lastb = ""
 
while True:
 
	b = img.read(1)
	if b == "":
	    result += lastb
	    break
 
	if lastb + b == "\xFF\xFE":
	    img.read(int(b2a_hex(img.read(2)), 16) - 2)
	    result += img.read()
	    break
	else:
	    result += lastb
	    lastb = b
 
img.close()
 
out = open(sys.argv[2],'wb')
out.write(result)
out.close()

The script basically searches for the JFIF comment header (xFFxEE) and strips the data out. Expect unexpected consequences if the images being processed do not contain JFIF comments!

Written by Nick

January 28th, 2009 at 6:29 pm

Posted in Code

Leave a Reply