def soften_composite(src_fn, dst_fn=None): tmp_file = ManagedTempFile.from_same_extension(src_fn) soften_gauss(src_fn, tmp_file.file_name) if dst_fn is None: dst_fn = src_fn args = ["convert"] args.append(src_fn) args.append(tmp_file.file_name) args.append("-compose") args.append("Blend") args.append("-define") args.append("compose:args=60,40%") args.append("-composite") # If we got a dest file, use it args.append(dst_fn) print 'going to execute: %s' % (args,) subp = subprocess.Popen(args, stdout=None, stderr=None, shell=False) subp.communicate() print 'Execute done, rc: %s' % (subp.returncode,) if not subp.returncode == 0: raise Exception('failed to form strong blur') # having some problems that looks like file isn't getting written to disk # monitoring for such errors # remove if I can root cause the source of these glitches for i in xrange(30): if os.path.exists(dst_fn): break if i == 0: print 'WARNING: soften missing strong blur dest file name %s, waiting a bit...' % (dst_fn,) time.sleep(0.1) else: raise Exception('Missing soften strong blur output file name %s' % dst_fn)
def run(self, source_file_name, dest_file_name = None): ''' http://www.imagemagick.org/Usage/convolve/#soft_blur convert face.png -morphology Convolve Gaussian:0x3 face_strong_blur.png convert face.png face_strong_blur.png \ -compose Blend -define compose:args=60,40% -composite \ face_soft_blur.png If dest_file_name is not given, done in place ''' strong_blur_mtemp_file = ManagedTempFile.from_same_extension(source_file_name) args = list() args.append(source_file_name) args.append("-morphology") args.append("Convolve") args.append("Gaussian:0x3") args.append(strong_blur_mtemp_file.file_name) (rc, output) = Execute.with_output("convert", args) if not rc == 0: raise Exception('failed to form strong blur') args = list() args.append(source_file_name) args.append(strong_blur_mtemp_file.file_name) args.append("-compose") args.append("Blend") args.append("-define") args.append("compose:args=60,40%") args.append("-composite") # If we got a dest file, use it if dest_file_name: args.append(dest_file_name) # Otherwise, overwrite else: args.append(source_file_name) (rc, output) = Execute.with_output("convert", args) if not rc == 0: raise Exception('failed to form strong blur')
def soften_composite(src_fn, dst_fn=None): tmp_file = ManagedTempFile.from_same_extension(src_fn) soften_gauss(src_fn, tmp_file.file_name) if dst_fn is None: dst_fn = src_fn args = ["convert"] args.append(src_fn) args.append(tmp_file.file_name) args.append("-compose") args.append("Blend") args.append("-define") args.append("compose:args=60,40%") args.append("-composite") # If we got a dest file, use it args.append(dst_fn) print 'going to execute: %s' % (args, ) subp = subprocess.Popen(args, stdout=None, stderr=None, shell=False) subp.communicate() print 'Execute done, rc: %s' % (subp.returncode, ) if not subp.returncode == 0: raise Exception('failed to form strong blur') # having some problems that looks like file isn't getting written to disk # monitoring for such errors # remove if I can root cause the source of these glitches for i in xrange(30): if os.path.exists(dst_fn): break if i == 0: print 'WARNING: soften missing strong blur dest file name %s, waiting a bit...' % ( dst_fn, ) time.sleep(0.1) else: raise Exception('Missing soften strong blur output file name %s' % dst_fn)
def do_generate_control_points_by_pair(self, pair, image_fn_pair): '''high level function uses by sub-stitches. Given a pair of images make a best effort to return a .pto object''' ''' pair: ImageCoordinatePair() object image_fn_pair: tuple of strings Algorithm: First try to stitch normally (either whole image or partial depending on the mode) If that doesn't succeed and softening is enabled try up to three times to soften to produce a match If that still doesn't produce a decent solution return None and let higher levels deal with ''' soften_iterations = 3 print print #print 'Generating project for image pair (%s / %s, %s / %s)' % (image_fn_pair[0], str(pair[0]), image_fn_pair[1], str(pair[1])) print 'Generating project for image pair (%s, %s)' % (image_fn_pair[0], image_fn_pair[1]) if True: # Try raw initially print 'Attempting sharp match...' ret_project = self.try_control_points_with_position(pair, image_fn_pair) if ret_project: return ret_project print 'WARNING: bad project, attempting soften...' soften_image_file_0_managed = ManagedTempFile.from_same_extension(image_fn_pair[0]) soften_image_file_1_managed = ManagedTempFile.from_same_extension(image_fn_pair[1]) print 'Soften fn0: %s' % soften_image_file_0_managed.file_name print 'Soften fn1: %s' % soften_image_file_1_managed.file_name for i in xrange(soften_iterations): self.soften_try[i] += 1 # And then start screwing with it # Wonder if we can combine features from multiple soften passes? # Or at least take the maximum # Do features get much less accurate as the soften gets up there? print 'Attempting soften %d / %d' % (i + 1, soften_iterations) if i == 0: soften_composite(image_fn_pair[0], soften_image_file_0_managed.file_name) soften_composite(image_fn_pair[1], soften_image_file_1_managed.file_name) else: soften_composite(soften_image_file_0_managed.file_name) soften_composite(soften_image_file_1_managed.file_name) pair_soften_image_file_names = (soften_image_file_0_managed.file_name, soften_image_file_1_managed.file_name) ret_project = self.try_control_points_with_position(pair, pair_soften_image_file_names) # Did we win? if ret_project: # Fixup the project to reflect the correct file names text = str(ret_project) if 0: print print 'Before sub' print print str(ret_project) print print print print '%s => %s' % (soften_image_file_0_managed.file_name, image_fn_pair[0]) text = text.replace(soften_image_file_0_managed.file_name, image_fn_pair[0]) print '%s => %s' % (soften_image_file_1_managed.file_name, image_fn_pair[1]) text = text.replace(soften_image_file_1_managed.file_name, image_fn_pair[1]) ret_project.set_text(text) if 0: print print 'After sub' print print str(ret_project) print print print #sys.exit(1) self.soften_ok[i] += 1 print 'Soften try: %s' % (self.soften_try,) print 'Soften ok: %s' % (self.soften_ok,) return ret_project print 'WARNING: gave up on generating control points!' return None
def do_generate_control_points_by_pair(self, pair, image_fn_pair): '''high level function uses by sub-stitches. Given a pair of images make a best effort to return a .pto object''' ''' pair: ImageCoordinatePair() object image_fn_pair: tuple of strings Algorithm: First try to stitch normally (either whole image or partial depending on the mode) If that doesn't succeed and softening is enabled try up to three times to soften to produce a match If that still doesn't produce a decent solution return None and let higher levels deal with ''' soften_iterations = 3 print print #print 'Generating project for image pair (%s / %s, %s / %s)' % (image_fn_pair[0], str(pair[0]), image_fn_pair[1], str(pair[1])) print 'Generating project for image pair (%s, %s)' % (image_fn_pair[0], image_fn_pair[1]) if True: # Try raw initially print 'Attempting sharp match...' ret_project = self.try_control_points_with_position( pair, image_fn_pair) if ret_project: return ret_project print 'WARNING: bad project, attempting soften...' soften_image_file_0_managed = ManagedTempFile.from_same_extension( image_fn_pair[0]) soften_image_file_1_managed = ManagedTempFile.from_same_extension( image_fn_pair[1]) print 'Soften fn0: %s' % soften_image_file_0_managed.file_name print 'Soften fn1: %s' % soften_image_file_1_managed.file_name for i in xrange(soften_iterations): self.soften_try[i] += 1 # And then start screwing with it # Wonder if we can combine features from multiple soften passes? # Or at least take the maximum # Do features get much less accurate as the soften gets up there? print 'Attempting soften %d / %d' % (i + 1, soften_iterations) if i == 0: soften_composite(image_fn_pair[0], soften_image_file_0_managed.file_name) soften_composite(image_fn_pair[1], soften_image_file_1_managed.file_name) else: soften_composite(soften_image_file_0_managed.file_name) soften_composite(soften_image_file_1_managed.file_name) pair_soften_image_file_names = ( soften_image_file_0_managed.file_name, soften_image_file_1_managed.file_name) ret_project = self.try_control_points_with_position( pair, pair_soften_image_file_names) # Did we win? if ret_project: # Fixup the project to reflect the correct file names text = str(ret_project) if 0: print print 'Before sub' print print str(ret_project) print print print print '%s => %s' % (soften_image_file_0_managed.file_name, image_fn_pair[0]) text = text.replace(soften_image_file_0_managed.file_name, image_fn_pair[0]) print '%s => %s' % (soften_image_file_1_managed.file_name, image_fn_pair[1]) text = text.replace(soften_image_file_1_managed.file_name, image_fn_pair[1]) ret_project.set_text(text) if 0: print print 'After sub' print print str(ret_project) print print print #sys.exit(1) self.soften_ok[i] += 1 print 'Soften try: %s' % (self.soften_try, ) print 'Soften ok: %s' % (self.soften_ok, ) return ret_project print 'WARNING: gave up on generating control points!' return None
def generate_control_points(self, pair, pair_images): soften_iterations = 3 if True: # Try raw initially ret_project = self.try_control_points(pair, pair_images) if ret_project: return ret_project print 'WARNING: bad project, attempting soften...' soften_image_file_0_managed = ManagedTempFile.from_same_extension(pair_images[0]) soften_image_file_1_managed = ManagedTempFile.from_same_extension(pair_images[1]) softener = Softener() first_run = True for i in range(0, soften_iterations): # And then start screwing with it # Wonder if we can combine features from multiple soften passes? # Or at least take the maximum # Do features get much less accurate as the soften gets up there? print 'Attempting soften %d / %d' % (i + 1, soften_iterations) if first_run: softener.run(pair_images[0], soften_image_file_0_managed.file_name) softener.run(pair_images[1], soften_image_file_1_managed.file_name) else: softener.run(soften_image_file_0_managed.file_name) softener.run(soften_image_file_1_managed.file_name) pair_soften_image_file_names = (soften_image_file_0_managed.file_name, soften_image_file_1_managed.file_name) ret_project = self.try_control_points(pair, pair_soften_image_file_names) # Did we win? if ret_project: # Fixup the project to reflect the correct file names text = ret_project.__repr__() print print 'Before sub' print print ret_project.__repr__() print print print print '%s => %s' % (soften_image_file_0_managed.file_name, pair_images[0]) text = text.replace(soften_image_file_0_managed.file_name, pair_images[0]) print '%s => %s' % (soften_image_file_1_managed.file_name, pair_images[1]) text = text.replace(soften_image_file_1_managed.file_name, pair_images[1]) ret_project.set_text(text) print print 'After sub' print print ret_project.__repr__() print print print #sys.exit(1) return ret_project first_run = False print 'WARNING: gave up on generating control points!' return None