Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 14th, 2012  |  syntax: Python  |  size: 0.85 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)