Beispiel #1
0
def create_first_task(form, images):
    GROUP_COUNT = int(form.getfirst('numberOfGroups', 5))
    ALPHABETS_PER_GROUP = int(form.getfirst('alphabetsPerGroup', 10))
    QUESTION_COUNT = int(form.getfirst('questionsPerGroup', 1))
    MAKE_DATA_URI = True
    alphabet_ids = tuple(_random.sample(images.keys(), GROUP_COUNT * ALPHABETS_PER_GROUP))
    TRAINING_CHARACTERS_PER_ALPHABET = int(form.getfirst('trainingCharactersPerAlphabet', 2))
    groups = [{'alphabets':dict((alphabet_id, []) for alphabet_id in alphabet_ids[ALPHABETS_PER_GROUP*i:ALPHABETS_PER_GROUP*(i+1)])} for i in range(GROUP_COUNT)]
    for group in groups:
        asking_alphabet_id = _random.choice(list(group['alphabets'].keys()))
        for alphabet_id in group['alphabets']:
            uids = list(images[alphabet_id].keys())
            image_numbers = list(range(len(images[alphabet_id][uids[0]])))
            use_uids = _random.sample(uids, TRAINING_CHARACTERS_PER_ALPHABET)
            use_image_numbers = _random.sample(image_numbers, TRAINING_CHARACTERS_PER_ALPHABET)
            group['alphabets'][alphabet_id] = [images[alphabet_id][uid][image_num] for uid, image_num in zip(use_uids, use_image_numbers)]
            if MAKE_DATA_URI:
                group['alphabets'][alphabet_id] = [{'url':urllib.parse.urljoin(urllib.parse.urljoin(BASE_URL, os.path.relpath(BASE_PATH, FROM_PATH)), image),
                                                    'data_uri':png_to_uri(os.path.join(FROM_PATH, image))} for image in group['alphabets'][alphabet_id]]
            if alphabet_id == asking_alphabet_id:
                uid, image_num = None, None
                question_uids = _random.sample([uid for uid in uids if uid not in use_uids], QUESTION_COUNT)
                question_image_nums = _random.sample([image_num for image_num in image_numbers if image_num not in use_image_numbers], QUESTION_COUNT)
                group['ask_for'] = [images[alphabet_id][uid][image_num] for uid, image_num in zip(question_uids, question_image_nums)]
                if MAKE_DATA_URI:
                    group['ask_for'] = [{'url':urllib.parse.urljoin(urllib.parse.urljoin(BASE_URL, os.path.relpath(BASE_PATH, FROM_PATH)), image),
                                         'data_uri':png_to_uri(os.path.join(FROM_PATH, image))} for image in group['ask_for']]
    
    return groups
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
Beispiel #4
0
def anonymize_image(image_path, from_path=BASE_PATH, from_url=BASE_URL, desired_base_path=BASE_PATH, include_data_uri=False, include_hash=True, include_original=True, **kwargs):
    name = os.path.split(image_path)[-1]
    image_path = os.path.relpath(os.path.join(from_path, image_path), desired_base_path)
    alphabet, number, uid = _image_reg.match(name).groups()
    first, second, third, fourth = (alphabet + 'a')[:4]
    uid_first, uid_second, uid_third, uid_fourth, uid_rest = uid[:2], uid[2:4], uid[4:6], uid[6:8], uid[8:]
##    print('RewriteRule ^results/accepted-images/(..)%(first)s(..)%(second)s(..)%(third)s([0-9][0-9])([^\.]+)\.png /~jgross/alphabets/results/accepted-images/%(alphabet)s/%(alphabet)s_$4_$1$2$3$5.png' % locals())
    rtn = {
           'anonymous url':urllib.parse.urljoin(ACCEPTED_IMAGES_URL, '%(uid_first)s%(first)s%(uid_second)s%(second)s%(uid_third)s%(third)s%(uid_fourth)s%(fourth)s%(number)s%(uid_rest)s.png' % locals())
          }
    if include_data_uri:
        rtn['data uri'] = png_to_uri(os.path.join(from_path, image_path))
    if include_hash:
        rtn['hash'] = hash(name)
    if include_original:
        rtn['original url'] = urllib.parse.urljoin(from_url, image_path)
    return rtn
Beispiel #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 print_image(image_path, image_url):
     if use_uri:
         print_method('<img src="' + png_to_uri(image_path) + '" height=100px />')
     else:
         print_method('<img src="' + image_url + '" height=100px />')
Beispiel #8
0
def create_first_task(form, images):
    GROUP_COUNT = int(form.getfirst('numberOfGroups', 5))
    ALPHABETS_PER_GROUP = int(form.getfirst('alphabetsPerGroup', 10))
    QUESTION_COUNT = int(form.getfirst('questionsPerGroup', 1))
    MAKE_DATA_URI = True
    alphabet_ids = tuple(
        _random.sample(images.keys(), GROUP_COUNT * ALPHABETS_PER_GROUP))
    TRAINING_CHARACTERS_PER_ALPHABET = int(
        form.getfirst('trainingCharactersPerAlphabet', 2))
    groups = [{
        'alphabets':
        dict((alphabet_id, [])
             for alphabet_id in alphabet_ids[ALPHABETS_PER_GROUP *
                                             i:ALPHABETS_PER_GROUP * (i + 1)])
    } for i in range(GROUP_COUNT)]
    for group in groups:
        asking_alphabet_id = _random.choice(list(group['alphabets'].keys()))
        for alphabet_id in group['alphabets']:
            uids = list(images[alphabet_id].keys())
            image_numbers = list(range(len(images[alphabet_id][uids[0]])))
            use_uids = _random.sample(uids, TRAINING_CHARACTERS_PER_ALPHABET)
            use_image_numbers = _random.sample(
                image_numbers, TRAINING_CHARACTERS_PER_ALPHABET)
            group['alphabets'][alphabet_id] = [
                images[alphabet_id][uid][image_num]
                for uid, image_num in zip(use_uids, use_image_numbers)
            ]
            if MAKE_DATA_URI:
                group['alphabets'][alphabet_id] = [{
                    'url':
                    urllib.parse.urljoin(
                        urllib.parse.urljoin(
                            BASE_URL, os.path.relpath(BASE_PATH, FROM_PATH)),
                        image),
                    'data_uri':
                    png_to_uri(os.path.join(FROM_PATH, image))
                } for image in group['alphabets'][alphabet_id]]
            if alphabet_id == asking_alphabet_id:
                uid, image_num = None, None
                question_uids = _random.sample(
                    [uid for uid in uids if uid not in use_uids],
                    QUESTION_COUNT)
                question_image_nums = _random.sample([
                    image_num for image_num in image_numbers
                    if image_num not in use_image_numbers
                ], QUESTION_COUNT)
                group['ask_for'] = [
                    images[alphabet_id][uid][image_num] for uid, image_num in
                    zip(question_uids, question_image_nums)
                ]
                if MAKE_DATA_URI:
                    group['ask_for'] = [{
                        'url':
                        urllib.parse.urljoin(
                            urllib.parse.urljoin(
                                BASE_URL,
                                os.path.relpath(BASE_PATH, FROM_PATH)), image),
                        'data_uri':
                        png_to_uri(os.path.join(FROM_PATH, image))
                    } for image in group['ask_for']]

    return groups
Beispiel #9
0
 def print_image(image_path, image_url):
     if use_uri:
         print_method('<img src="' + png_to_uri(image_path) +
                      '" height=100px />')
     else:
         print_method('<img src="' + image_url + '" height=100px />')