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
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
Esempio n. 3
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