def save( self ): """Writes metadata out to photo comments.""" # load existing comments c = jpeg.getComments( self.fname ) # Find previous metadata and get comments that precede and follow # our metadata (so we don't nuke somebody else's comments). before = '' after = '' i = c.find( BEGIN_TAG ) if i == -1: # No previous tags before = c else: # Get before before = c[:i] # And get after i = c.find( END_TAG ) assert i != -1, "Bad metadata" after = c[i+len( END_TAG ):] # Generate metadata block meta = BEGIN_TAG for ( name, value ) in self.items(): meta = '%s<%s>%s</%s>' % ( meta, name, value, name ) meta = '%s%s' % ( meta, END_TAG ) # Write comments back out jpeg.setComments( '%s%s%s' % ( before, meta, after ), self.fname )
def match_thumb(fb_thumbs, loc_thumbs, picdir): ''' Match local thumbnails to facebook thumbnails. The algorithm used here is quite rough and ready, but reasonably reliable and performant. Local thumbnails are indexed, based on a sample spot. As each facebook thumbnail is examined, the most likely matches are those with the closest colour on the sample spot. Image comparison is done by the average sum of squares difference, for each colour and pixel. The cutoff value is determined by experiment. ''' diff = lambda a,b: (a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2 def img_diff(x, y): xd = x.getdata() yd = y.getdata() if len(yd) > len(xd): yd = list(yd)[:len(xd)] if len(xd) > len(yd): xd = list(xd)[:len(yd)] return reduce(operator.add, map(diff, xd, yd)) / len(xd) samp_idx = {} for t in loc_thumbs: samp_idx.setdefault(t.img.getpixel(samp_spot), []).append(t) matches = [] for i,fb_thumb in enumerate(fb_thumbs): if options.verbose: print "Matching... [%d/%d]" % (i+1, len(fb_thumbs)) likely = [(diff(fb_thumb.img.getpixel(samp_spot), s), samp_idx[s]) for s in samp_idx] likely.sort(key = lambda x: x[0]) match = None for i,(x,y) in enumerate(likely): for loc_thumb in y: qqq = img_diff(fb_thumb.img, loc_thumb.img) if qqq < match_cutoff: match = loc_thumb break if match: break if match: matches.append((fb_thumb, loc_thumb, qqq, i)) else: print "Warning - no match for %s" % fb_thumb.fname matches.sort(key = lambda x: x[1].orig_name.lower()) for ft,lt,x,y in matches: jpeg.setComments(ft.fburl, os.path.join(picdir, lt.orig_name)) fh = open(os.path.join(workdir, 'check.html'), 'w') tmpl = '<img src="%s"/><img src="%s"/>%d %d<br/>' cont = '\n'.join(tmpl % (ft.fname, lt.fname, qqq, y) for ft,lt,qqq,y in matches) fh.write('<html><body>%s</body></html>' % cont) fh.close()
def setJpegInfo(path, info): """ @summary: Set exif information into jpef file. @param path: Path where information will be set. @param info: Tuple with exif info, exif2 info and comments. """ infoExif, infoExif2, infoComments = info if infoExif != None: jpeg.setExif(infoExif, path) else: __log__.warning("There is not EXIF information.") if infoExif2 != None: jpeg.setExif2(infoExif2, path) else: __log__.warning("There is not EXIF2 information.") if infoComments != None: jpeg.setComments(infoComments, path) else: __log__.warning("There are not comments.")