1. #!/usr/bin/python26
  2.  
  3.  
  4. import ImageChops
  5. from PIL import Image
  6. import urllib2, urllib
  7. from StringIO import StringIO
  8. import sys, os
  9.  
  10.  
  11. def equal(im1, im2):
  12.     return ImageChops.difference(im1, im2).getbbox() is None
  13.    
  14. im = "http://www.poltz.com/blognews/this-is-only-a-test.jpg"
  15. #im = "http://www.mediabistro.com/unbeige/files/original/fill%20in%20the%20bubble.jpg"
  16. outfile = "/foo/bar/test.jpg"
  17.  
  18. try:
  19.     remote = urllib2.urlopen(im)
  20.     im1 = Image.open(StringIO(remote.read()))
  21. except Exception, e:
  22.     print 'Unable to fetch image %s: %s' % (im, e)
  23.  
  24.    
  25. if os.path.isfile(outfile):
  26.     im2 = Image.open(outfile)
  27.     if (equal(im1, im2)):
  28.         print "no new img"
  29.     else:
  30.         print "new img"
  31.         os.remove(outfile)
  32.         im1.save(outfile,quality=100)
  33. else:
  34.     print "%s was a new pic" % (outfile)
  35.     im1.save(outfile,quality=100)