Exemple #1
0
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 range(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)
Exemple #2
0
    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