"""
screenStitch.py

Will ask for files, find the region that changed between first two files,
crop the changes, and stitch a final image together.

Outputs a new stitch.png in the directory of first selected image and will
overwrite any existing files with same name.

By jaflo at github: https://github.com/jaflo/screenStitch/
Licensed under the MIT license (http://opensource.org/licenses/MIT)

If you make any changes or use this in a project, I would appreciate
if you tell me. Please leave this notice intact.
"""

from PIL import Image, ImageChops
from Tkinter import Tk
from tkFileDialog import askopenfilenames
import subprocess
import platform
import shutil
import os

if platform.system()=="Windows":
	foldersep = "\\"
else:
	foldersep = "/"
print "Made by jaflo on github"
Tk().withdraw()
files = askopenfilenames(title="Choose images")
print "Choose images to compare"
images = []
i = 0
imagergbprev = 0
leftmost = 1e9
rightmost = 0
topmost = 1e9
bottommost = 0

if not os.path.exists("stitchtmp"):
    os.makedirs("stitchtmp")

for file in files:
	image = Image.open(file)
	imagergb = image.convert("RGB")
	if i == 1:
		print "Performing analysis"
		width, height = image.size
		for x in xrange(1, width):
			for y in xrange(1, height):
				r, g, b = imagergb.getpixel((x, y))
				if imagergb.getpixel((x, y)) != imagergbprev.getpixel((x, y)):
					#print "%d, %d" % (x, y)
					if leftmost > x:
						leftmost = x
					if rightmost < x:
						rightmost = x
					if topmost > y:
						topmost = y
					if bottommost < y:
						bottommost = y
		print "Differences at (%d, %d) to (%d, %d)" % (leftmost, rightmost, topmost, bottommost)
	if i > 0:
		if i == 1:
			imagergbprev.crop((leftmost, topmost, rightmost, bottommost)).save("stitchtmp"+foldersep+"diff0.png")
		else:
			imagergbprev.crop((leftmost, topmost, rightmost, bottommost)).save("stitchtmp"+foldersep+"diff"+str(i-1)+".png")
		if i == (len(files)-1):
			imagergb.crop((leftmost, topmost, rightmost, bottommost)).save("stitchtmp"+foldersep+"diff"+str(i)+".png")
	imagergbprev = imagergb
	i+=1

total = i + 1
i = 0
fullimage = Image.new("RGB", (rightmost-leftmost-11, 9999), "black")
Image.new("RGB", (rightmost-leftmost, 2000), "black").save("stitchtmp"+foldersep+"diff"+str(total-1)+".png")
lineonnew = 0

print "Stitching"
for num in range(0, total):
	if i > 0:
		image1 = Image.open("stitchtmp"+foldersep+"diff"+str(i-1)+".png")
		image2 = Image.open("stitchtmp"+foldersep+"diff"+str(i)+".png")
		image1rgb = image1.convert("RGB")
		image2rgb = image2.convert("RGB")
		width, height = image1.size
		sodone = False
		for line in xrange(0, height):
			im1 = image1rgb.crop((0, line, width, line+1))
			im2 = image2rgb.crop((0, 1, width, 2))
			if ImageChops.difference(im1, im2).getbbox() is None:
				sodone = True
			else:
				if not sodone:
					fullimage.paste(im1, (0,lineonnew))
					lineonnew+=1
	i+=1

print "Cropping"
bg = Image.new(fullimage.mode, fullimage.size, (0,0,0))
diff = ImageChops.difference(fullimage, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
	fullimage = fullimage.crop(bbox)

if platform.system()=="Windows":
	fullimage.save(files[0][0:files[0].rfind(foldersep)]+foldersep+"stitch.png")
	subprocess.Popen(r'explorer /select,"'+files[0][0:files[0].rfind(foldersep)]+foldersep+'stitch.png"')
else:
	fullimage.save(files[0][0:files[0].rfind(foldersep)]+foldersep+"stitch.png")
	if platform.system()=="Darwin":
		subprocess.call(["open", "-R", files[0][0:files[0].rfind(foldersep)]+foldersep+"stitch.png"])

shutil.rmtree("stitchtmp")
print "Done!"

@@ -553,17 +553,17 @@
 ps%0Afrom 
-T
+t
 kinter i
@@ -582,13 +582,19 @@
 m tk
-FileD
+inter.filed
 ialo
@@ -756,40 +756,8 @@
 %22/%22%0A
-print %22Made by jaflo on github%22%0A
 Tk()
@@ -821,17 +821,17 @@
 %22)%0Aprint
- 
+(
 %22Choose 
@@ -848,16 +848,17 @@
 compare%22
+)
 %0Aimages 
@@ -1107,17 +1107,17 @@
 %0A%09%09print
- 
+(
 %22Perform
@@ -1129,16 +1129,17 @@
 nalysis%22
+)
 %0A%09%09width
@@ -1167,25 +1167,24 @@
 %0A%09%09for x in 
-x
 range(1, wid
@@ -1196,25 +1196,24 @@
 %09%09%09for y in 
-x
 range(1, hei
@@ -1336,17 +1336,17 @@
 %09%09#print
- 
+(
 %22%25d, %25d%22
@@ -1354,16 +1354,17 @@
 %25 (x, y)
+)
 %0A%09%09%09%09%09if
@@ -1531,17 +1531,17 @@
 %0A%09%09print
- 
+(
 %22Differe
@@ -1614,16 +1614,17 @@
 tommost)
+)
 %0A%09if i %3E
@@ -2255,17 +2255,17 @@
 0%0A%0Aprint
- 
+(
 %22Stitchi
@@ -2267,16 +2267,17 @@
 itching%22
+)
 %0Afor num
@@ -2575,17 +2575,16 @@
 line in 
-x
 range(0,
@@ -2859,17 +2859,17 @@
 1%0A%0Aprint
- 
+(
 %22Croppin
@@ -2870,16 +2870,17 @@
 ropping%22
+)
 %0Abg = Im
@@ -3547,13 +3547,14 @@
 rint
- 
+(
 %22Done!%22
+)
 %0A

Update to support Python 3