def put_extras(path, form_dict, uid, quiet=True):
    html_file_name = os.path.join(path, uid.replace('-', 'm') + '_drawings.html')
    image_file_base_name = os.path.join(path, uid.replace('-', 'm') + '_image_')
    stroke_file_base_name = os.path.join(path, uid.replace('-', 'm') + '_stroke_')

    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    html = [r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Images Drawn By %s</title>
    <style type="text/css">
        div {
            display: inline-block;
            border: thin solid black;
        }
    </style>
</head>
<body>""" % uid]

    for n, task in tasks_data.iteritems():
        image_name = image_file_base_name + '%d.png' % (int(n) + 1)
        uri_to_png(task['image'], '%s%d.png' % (image_file_base_name, int(n) + 1))
        html.append(r"""<div><img src="%s" alt="given" title="given"/><img src="%s_image_%d.png" alt="drawn" title="drawn"/></div>""" %
                    (task['completion-image-anonymous_url'], uid.replace('-', 'm'), int(n) + 1))
        with open('%s%d.cstroke' % (stroke_file_base_name, int(n) + 1), 'wb') as f:
            f.write(compress_stroke(task['strokes']))

    html.append(r"""</body>
</html>""")
    with open(html_file_name, 'w') as f:
        f.write('\n'.join(html))
def compress_all_strokes_in_current_directory(uncompressed_ext='.stroke', compressed_ext='.cstroke', remove_old=True, verbose=True, show_compression=True):
    for base, dirs, files in os.walk(os.getcwd()):
        if verbose: print("I'm in " + base)
        for file_name in files:
            if file_name[-len(uncompressed_ext):] == uncompressed_ext:
                if not show_compression: print('Compressing %s...' % file_name)
                with open(os.path.join(base, file_name), 'rb') as f:
                    stroke = f.read()
                new_stroke = compress_stroke(stroke)
                with open(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext)), 'wb') as f:
                    f.write(new_stroke)
                if show_compression: print('Compressed %s at\t\t%.2f%%' % (file_name, 100.0 * len(new_stroke) / len(stroke)))
                if remove_old:
                    os.rename(os.path.join(base, file_name), os.path.join(base, file_name + '.bak'))
                    # Testing to make sure that we can actually decompress the new file correctly before deleting the old file
                    with open(os.path.join(base, file_name + '.bak'), 'rb') as f:
                        old_stroke = f.read()
                    with open(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext)), 'rb') as f:
                        new_stroke = f.read()
                    new_old_stroke = decompress_stroke(new_stroke)
                    if new_old_stroke.strip() != old_stroke.strip() and stroke_to_list(new_old_stroke) != stroke_to_list(old_stroke):
                        os.rename(os.path.join(base, file_name + '.bak'), os.path.join(base, file_name))
                        os.remove(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext)))
                        print('Error on stroke %s in %s' % (file_name, base))
                        print('I wanted:')
                        print(stroke_to_list(old_stroke))
                        print('I got:')
                        print(stroke_to_list(new_old_stroke))
                        print('The compressed version is:')
                        print(repr(new_stroke))
                        raw_input('Press enter or ^C')
                    else:
                        os.remove(os.path.join(base, file_name + '.bak'))
Example #3
0
def decompress_all_strokes_in_current_directory(uncompressed_ext='.stroke',
                                                compressed_ext='.cstroke',
                                                remove_old=True,
                                                verbose=True,
                                                show_compression=True):
    for base, dirs, files in os.walk(os.getcwd()):
        if verbose: print("I'm in " + base)
        for file_name in files:
            if file_name[-len(compressed_ext):] == compressed_ext:
                if not show_compression:
                    print('Decompressing %s...' % file_name)
                with open(os.path.join(base, file_name), 'rb') as f:
                    stroke = f.read()
                new_stroke = decompress_stroke(stroke)
                with open(
                        os.path.join(
                            base,
                            file_name.replace(compressed_ext,
                                              uncompressed_ext)), 'wb') as f:
                    f.write(new_stroke)
                if show_compression:
                    print('Decompressed %s at\t\t%.2f%%' %
                          (file_name, 100.0 * len(stroke) / len(new_stroke)))

                if remove_old:
                    os.rename(os.path.join(base, file_name),
                              os.path.join(base, file_name + '.bak'))
                    # Testing to make sure that we can actually decompress the new file correctly before deleting the old file
                    with open(os.path.join(base, file_name + '.bak'),
                              'rb') as f:
                        old_stroke = f.read()
                    with open(
                            os.path.join(
                                base,
                                file_name.replace(compressed_ext,
                                                  uncompressed_ext)),
                            'rb') as f:
                        new_stroke = f.read()
                    new_old_stroke = compress_stroke(new_stroke)
                    if new_old_stroke != old_stroke and stroke_to_list(
                            new_old_stroke) != stroke_to_list(old_stroke):
                        os.rename(os.path.join(base, file_name + '.bak'),
                                  os.path.join(base, file_name))
                        os.remove(
                            os.path.join(
                                base,
                                file_name.replace(uncompressed_ext,
                                                  compressed_ext)))
                        print('Error on stroke %s in %s' % (file_name, base))
                        print('I wanted:')
                        print(repr(old_stroke))
                        print('I got:')
                        print(repr(new_old_stroke))
                        print('The uncompressed version is:')
                        print(new_stroke)
                        raw_input('Press enter or ^C')
                    else:
                        os.remove(os.path.join(base, file_name + '.bak'))
def put_extras(path, form_dict, uid, quiet=True):
    html_file_name = os.path.join(path,
                                  uid.replace('-', 'm') + '_drawings.html')
    image_file_base_name = os.path.join(path,
                                        uid.replace('-', 'm') + '_image_')
    stroke_file_base_name = os.path.join(path,
                                         uid.replace('-', 'm') + '_stroke_')

    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    html = [
        r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Images Drawn By %s</title>
    <style type="text/css">
        div {
            display: inline-block;
            border: thin solid black;
        }
    </style>
</head>
<body>""" % uid
    ]

    for n, task in tasks_data.iteritems():
        image_name = image_file_base_name + '%d.png' % (int(n) + 1)
        uri_to_png(task['image'],
                   '%s%d.png' % (image_file_base_name, int(n) + 1))
        html.append(
            r"""<div><img src="%s" alt="given" title="given"/><img src="%s_image_%d.png" alt="drawn" title="drawn"/></div>"""
            % (task['completion-image-anonymous_url'], uid.replace(
                '-', 'm'), int(n) + 1))
        with open('%s%d.cstroke' % (stroke_file_base_name, int(n) + 1),
                  'wb') as f:
            f.write(compress_stroke(task['strokes']))

    html.append(r"""</body>
</html>""")
    with open(html_file_name, 'w') as f:
        f.write('\n'.join(html))
Example #5
0
def put_extras(path, form_dict, uid, quiet=True):
    html_file_name = os.path.join(path, uid.replace('-', 'm') + '_drawings.html')
    image_file_base_name = os.path.join(path, uid.replace('-', 'm') + '_image_')
    stroke_file_base_name = os.path.join(path, uid.replace('-', 'm') + '_stroke_')

    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    html = [r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Images Drawn By %s</title>
    <style type="text/css">
        div.task {
            display: inline-block;
            border: thin solid black;
        }

        .image-holder {
              padding: 2.5px;
              width: 100px;
              height: 100px;
        }

        .class-holder {
              display: inline-block;
              padding: 0px;
        }

        .class-box {
              display: inline-block;
              padding: 0px;
        }

        .image-box {
              margin: 2.5px;
              border: thin solid #C0C0C0; //lightgray
              text-align: center;
              vertical-align: middle;
              display: block;
        }

        .class-box-untimed {
              border: thin black solid;
        }

    </style>
</head>
<body>""" % uid]

    for n, task in tasks_data.iteritems():
        image_name = image_file_base_name + '%d.png' % (int(n) + 1)
        uri_to_png(task['image'], '%s%d.png' % (image_file_base_name, int(n) + 1))
        drawn_image = r"""<img src="%s_image_%d.png" alt="drawn" title="drawn"/>""" % (uid.replace('-', 'm'), int(n) + 1)
        if 'class-0-example-image-1-anonymous_url' in task or \
           'class-1-example-image-0-anonymous_url' in task: # untimed, multiple images
            task_html = []
            class_urls = {}
            for key, value in task.iteritems():
                if '-anonymous_url' not in key: continue
                match = CLASS_REGEX.match(key)
                if not match: continue
                class_i, example_i = map(int, match.groups())
                if class_i not in class_urls: class_urls[class_i] = {}
                class_urls[class_i][example_i] = value
            for class_i in sorted(class_urls.keys()):
                task_html.append('<div class="class-box class-box-untimed">')
                for example_i in sorted(class_urls[class_i].keys()):
                    task_html.append(r"""<div class="image-box">
<span class="wraptocenter image-holder">
<img src="%s" />
</span>
</div>""" % class_urls[class_i][example_i])
                task_html.append('</div>')
            task_html.append(r'<br />')
            task_html.append(drawn_image)
            task_html = '\n'.join(task_html)
        else: # timed, single image
            task_html = r"""<img src="%s" alt="given" title="given"/>%s</div>""" % (task['class-0-example-image-0-anonymous_url'], drawn_image)
        html.append(r"""<div class="task">%s</div>""" % task_html)
        with open('%s%d.cstroke' % (stroke_file_base_name, int(n) + 1), 'wb') as f:
            f.write(compress_stroke(task['strokes']))

    html.append(r"""</body>
</html>""")
    with open(html_file_name, 'w') as f:
        f.write('\n'.join(html))
def put_extras(path, form_dict, uid, quiet=True):
    html_file_name = os.path.join(path, uid.replace("-", "m") + "_drawings.html")
    image_file_base_name = os.path.join(path, uid.replace("-", "m") + "_image_")
    stroke_file_base_name = os.path.join(path, uid.replace("-", "m") + "_stroke_")

    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)["task"]
    html = [
        r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Images Drawn By %s</title>
    <style type="text/css">
        div.task {
            display: inline-block;
            border: thin solid black;
        }

        .image-holder {
              padding: 2.5px;
              width: 100px;
              height: 100px;
        }

        .class-holder {
              display: inline-block;
              padding: 0px;
        }

        .class-box {
              display: inline-block;
              padding: 0px;
        }

        .image-box {
              margin: 2.5px;
              border: thin solid #C0C0C0; //lightgray
              text-align: center;
              vertical-align: middle;
              display: block;
        }

        .class-box-untimed {
              border: thin black solid;
        }

    </style>
</head>
<body>"""
        % uid
    ]

    for n, task in tasks_data.iteritems():
        image_name = image_file_base_name + "%d.png" % (int(n) + 1)
        uri_to_png(task["image"], "%s%d.png" % (image_file_base_name, int(n) + 1))
        drawn_image = r"""<img src="%s_image_%d.png" alt="drawn" title="drawn"/>""" % (
            uid.replace("-", "m"),
            int(n) + 1,
        )
        if (
            "class-0-example-image-1-anonymous_url" in task or "class-1-example-image-0-anonymous_url" in task
        ):  # untimed, multiple images
            task_html = []
            class_urls = {}
            for key, value in task.iteritems():
                if "-anonymous_url" not in key:
                    continue
                match = CLASS_REGEX.match(key)
                if not match:
                    continue
                class_i, example_i = map(int, match.groups())
                if class_i not in class_urls:
                    class_urls[class_i] = {}
                class_urls[class_i][example_i] = value
            for class_i in sorted(class_urls.keys()):
                task_html.append('<div class="class-box class-box-untimed">')
                for example_i in sorted(class_urls[class_i].keys()):
                    task_html.append(
                        r"""<div class="image-box">
<span class="wraptocenter image-holder">
<img src="%s" />
</span>
</div>"""
                        % class_urls[class_i][example_i]
                    )
                task_html.append("</div>")
            task_html.append(r"<br />")
            task_html.append(drawn_image)
            task_html = "\n".join(task_html)
        else:  # timed, single image
            task_html = r"""<img src="%s" alt="given" title="given"/>%s</div>""" % (
                task["class-0-example-image-0-anonymous_url"],
                drawn_image,
            )
        html.append(r"""<div class="task">%s</div>""" % task_html)
        with open("%s%d.cstroke" % (stroke_file_base_name, int(n) + 1), "wb") as f:
            f.write(compress_stroke(task["strokes"]))

    html.append(
        r"""</body>
</html>"""
    )
    with open(html_file_name, "w") as f:
        f.write("\n".join(html))