Exemplo n.º 1
0
def convert_all_alphabet_strokes_to_images(strokes=None, original_images=None, dest_images=None, verbose=True,
                                           auto_resize=True, new_max_dimen=100, **kargs):
    if strokes is None or dest_images is None or original_images is None:
        if verbose: print('Getting list of alphabets, images, and strokes...')
        if strokes is None:
            if verbose: print('Getting list of accepted strokes...')
            strokes = get_accepted_stroke_list(from_path='.')
        if dest_images is None: 
            if verbose: print('Getting list of accepted images...')
            dest_images = get_accepted_image_list(from_path='.')
        if original_images is None: 
            if verbose: print('Getting list of original images...')
            actual_original_images = get_original_image_list(from_path='.')
        if verbose: print('Done getting list of alphabets, images, and strokes.')
    for alphabet in strokes:
        if verbose: print("Alphabet: %s" % alphabet)
        for uid in strokes[alphabet]:
            if verbose: print(" Id: %s" % uid)
            if original_images is None:
                cur_original_image_sizes = [Image.open(image).size for image in actual_original_images[alphabet]]
                cur_original_image_sizes = [(int(math.ceil(width * 100.0 / height)), 100) for width, height in cur_original_image_sizes]
            else:
                cur_original_image_sizes = original_image_sizes[alphabet][uid]
            convert_strokes_to_images(strokes[alphabet][uid], dest_images[alphabet][uid], original_image_sizes=cur_original_image_sizes,
                                      verbose=verbose, auto_resize=auto_resize, new_max_dimen=new_max_dimen, **kargs)
            compress_images(image_list=dest_images[alphabet][uid], do_color_change=False)
def fixup_images(form_dict, width=105, height=105):
    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    image_dict = {}
    for n, task in tasks_data.iteritems():
        original_strokes_path = alphabets_strokes_dict[
            task['completion-image-alphabet']][task['completion-image-id']][
                int(task['completion-image-character-number']) - 1]
        image_half = task['image-show_half']
        strokes = task['strokes']

        image = cairo.ImageSurface.create_from_png(alphabets_images_dict[
            task['completion-image-alphabet']][task['completion-image-id']][
                int(task['completion-image-character-number']) - 1])
        ctx = cairo.Context(image)

        # unpaint the half-image

        # paint white
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        if image_half == 'top':
            ctx.rectangle(0, height / 2, width, height)
        elif image_half == 'bottom':
            ctx.rectangle(0, 0, width, height / 2)
        elif image_half == 'left':
            ctx.rectangle(width / 2, 0, width, height)
        elif image_half == 'right':
            ctx.rectangle(0, 0, width / 2, height)
        ctx.fill()

        # and clip
        if image_half == 'top':
            ctx.rectangle(0, height / 2 - 2.5, width, height)
        elif image_half == 'bottom':
            ctx.rectangle(0, 0, width, height / 2 + 2.5)
        elif image_half == 'left':
            ctx.rectangle(width / 2 - 2.5, 0, width, height)
        elif image_half == 'right':
            ctx.rectangle(0, 0, width / 2 + 2.5, height)
        ctx.clip()

        image, ctx = htmlcanvas.paint_image_with_strokes_cairo(strokes,
                                                               size=(width,
                                                                     height),
                                                               offset=0,
                                                               image=image,
                                                               ctx=ctx,
                                                               clear=False)

        with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
            image.write_to_png(f)
            image_dict['task-%s-image' % int(n)] = f.name

    compress_images(image_list=image_dict.values(), do_color_change=False)
    for key, path in image_dict.iteritems():
        form_dict[key] = png_to_uri(path)
        os.remove(path)

    return form_dict
Exemplo n.º 3
0
def record_submission(form_dict,
                      names=('get_unreviewed_image_list',
                             'get_unreviewed_stroke_list'),
                      many_dirs=False,
                      path=UNREVIEWED_PATH,
                      images_path=TURK_IMAGES_PATH,
                      strokes_path=TURK_STROKES_PATH,
                      extra_info_path=TURK_EXTRA_INFO_PATH,
                      verbose=True,
                      pseudo=False,
                      compress=False):
    if names:
        for name in names:
            raise_object_changed(name)
    if verbose: print('Hashing IP address...')
    uid = make_uid(form_dict)
    if many_dirs:
        print('Done<br>Making folder for your submission...')
        images_path = strokes_path = extra_info_path = folder = _make_folder_for_submission(
            uid, path=path)
    if verbose: print('Done<br>Retrieving and processing your drawings...')
    image_dict_list = _get_image_dict_list(form_dict)
    if verbose: print('Done<br>Saving your stroke and image data...')
    if many_dirs:
        make_base_file_name = None
    else:

        def make_base_file_name(image_name, uid):
            global alphabet_id
            if not alphabet_id or image_name[:len(alphabet_id)] != alphabet_id:
                alphabet_id = get_alphabet_id_from_file_name(image_name + '_' +
                                                             uid)
            return os.path.join(alphabet_id, image_name + '_' + uid)

    image_list = _save_images(image_dict_list,
                              uid,
                              images_path,
                              strokes_path,
                              make_base_file_name=make_base_file_name)
    if verbose: print('Done<br>Storing your non-image responses...')
    if many_dirs:
        _put_properties(extra_info_path, form_dict)
    else:
        file_name = get_alphabet_id_from_dict(
            form_dict) + '_' + uid + '.results.txt'
        _put_properties(extra_info_path, form_dict, file_name=file_name)
    if not pseudo and compress:
        if verbose: print('Done<br>Compressing your image data...')
        compress_images(folder=images_path, image_list=image_list)
    if many_dirs:
        _log_success(folder)
    if verbose: print('Done<br>You may now leave this page.<br>')
    if verbose:
        print(
            '<a href="http://scripts.mit.edu/~jgross/alphabets/">Return to home page</a>'
        )
def fixup_images(form_dict, width=105, height=105):
    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    image_dict = {}
    for n, task in tasks_data.iteritems():
        original_strokes_path = alphabets_strokes_dict[task['completion-image-alphabet']][task['completion-image-id']][int(task['completion-image-character-number']) - 1]
        image_half = task['image-show_half']
        strokes = task['strokes']

        image = cairo.ImageSurface.create_from_png(alphabets_images_dict[task['completion-image-alphabet']][task['completion-image-id']][int(task['completion-image-character-number']) - 1])
        ctx = cairo.Context(image)

        # unpaint the half-image

        # paint white
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        if image_half == 'top':
            ctx.rectangle(0, height / 2, width, height)
        elif image_half == 'bottom':
            ctx.rectangle(0, 0, width, height / 2)
        elif image_half == 'left':
            ctx.rectangle(width / 2, 0, width, height)
        elif image_half == 'right':
            ctx.rectangle(0, 0, width / 2, height)
        ctx.fill()

        # and clip
        if image_half == 'top':
            ctx.rectangle(0, height / 2 - 2.5, width, height)
        elif image_half == 'bottom':
            ctx.rectangle(0, 0, width, height / 2 + 2.5)
        elif image_half == 'left':
            ctx.rectangle(width / 2 - 2.5, 0, width, height)
        elif image_half == 'right':
            ctx.rectangle(0, 0, width / 2 + 2.5, height)
        ctx.clip()

        image, ctx = htmlcanvas.paint_image_with_strokes_cairo(strokes, size=(width,height), offset=0, image=image, ctx=ctx, clear=False)
        
        with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
            image.write_to_png(f)
            image_dict['task-%s-image' % int(n)] = f.name

    compress_images(image_list=image_dict.values(), do_color_change=False)
    for key, path in image_dict.iteritems():
        form_dict[key] = png_to_uri(path)
        os.remove(path)

    return form_dict
Exemplo n.º 5
0
def fixup_images(form_dict, width=100, height=100, offset=None):
    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)['task']
    image_dict = {}
    for n, task in tasks_data.iteritems():
        strokes = task['strokes']

        image, ctx = htmlcanvas.paint_image_with_strokes_cairo(strokes, size=(width,height), offset=offset)
        
        with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
            image.write_to_png(f)
            image_dict['task-%s-image' % int(n)] = f.name

    compress_images(image_list=image_dict.values(), do_color_change=False)
    for key, path in image_dict.iteritems():
        form_dict[key] = png_to_uri(path)
        os.remove(path)

    return form_dict
def fixup_images(form_dict, width=100, height=100, offset=None):
    tasks_data = turkutil.form_dict_to_tasks_dict(form_dict)["task"]
    image_dict = {}
    for n, task in tasks_data.iteritems():
        strokes = task["strokes"]

        image, ctx = htmlcanvas.paint_image_with_strokes_cairo(strokes, size=(width, height), offset=offset)

        with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
            image.write_to_png(f)
            image_dict["task-%s-image" % int(n)] = f.name

    compress_images(image_list=image_dict.values(), do_color_change=False)
    for key, path in image_dict.iteritems():
        form_dict[key] = png_to_uri(path)
        os.remove(path)

    return form_dict
def record_submission(form_dict, names=('get_unreviewed_image_list', 'get_unreviewed_stroke_list'),
                      many_dirs=False, path=UNREVIEWED_PATH,
                      images_path=TURK_IMAGES_PATH, strokes_path=TURK_STROKES_PATH, extra_info_path=TURK_EXTRA_INFO_PATH,
                      verbose=True, pseudo=False, compress=False):
    if names:
        for name in names:
            raise_object_changed(name)
    if verbose: print('Hashing IP address...')
    uid = make_uid(form_dict)
    if many_dirs:
        print('Done<br>Making folder for your submission...')
        images_path = strokes_path = extra_info_path = folder = _make_folder_for_submission(uid, path=path)
    if verbose: print('Done<br>Retrieving and processing your drawings...')
    image_dict_list = _get_image_dict_list(form_dict)
    if verbose: print('Done<br>Saving your stroke and image data...')
    if many_dirs:
        make_base_file_name = None
    else:
        def make_base_file_name(image_name, uid):
            global alphabet_id
            if not alphabet_id or image_name[:len(alphabet_id)] != alphabet_id:
                alphabet_id = get_alphabet_id_from_file_name(image_name + '_' + uid)
            return os.path.join(alphabet_id, image_name + '_' + uid)
    image_list = _save_images(image_dict_list, uid, images_path, strokes_path, make_base_file_name=make_base_file_name)
    if verbose: print('Done<br>Storing your non-image responses...')
    if many_dirs:
        _put_properties(extra_info_path, form_dict)
    else:
        file_name = get_alphabet_id_from_dict(form_dict) + '_' + uid + '.results.txt'
        _put_properties(extra_info_path, form_dict, file_name=file_name)
    if not pseudo and compress:
        if verbose: print('Done<br>Compressing your image data...')
        compress_images(folder=images_path, image_list=image_list)
    if many_dirs:
        _log_success(folder)
    if verbose: print('Done<br>You may now leave this page.<br>')
    if verbose: print('<a href="//jgross.scripts.mit.edu/alphabets/">Return to home page</a>')