def make_guesses(src_paths,
                 width,
                 variants,
                 offset,
                 candidates_path="/dev/shm/candidates",
                 guess_path="/dev/shm/guesses",
                 fresh=True,
                 **kwargs):
    candidates = make_candidates(width,
                                 variants,
                                 candidate_path=candidates_path,
                                 **kwargs)
    with contextlib.suppress(FileExistsError):
        os.makedirs(guess_path)

    arglists = []
    for sp in src_paths:
        for c in candidates:
            filename = "guess_%s_%s.png" % (sp.split("out_")[-1].split(
                ".png")[0], c.split('candidate-')[-1].split('.')[0])
            arglists.append([os.path.join(guess_path, filename), c, sp])
    with pygpar.PP(
            f"composite -gravity northwest -geometry +{offset*ai_han_solo.LETTER_WIDTH}+0 {{{2}}} {{{3}}} {{{1}}}",
            filter_exists=not fresh,
            eta=True,
            jobs=40) as _p:
        _p.queue_list(arglists)

    return next(iter(zip(*arglists)))
def make_candidates(width,
                    variants,
                    candidate_path="/dev/shm/candidates",
                    fonts_path="emnist-png/emnist-balanced"):
    with contextlib.suppress(FileExistsError):
        os.makedirs(candidate_path)
    possibilities = list(itertools.product(ALPHABET, repeat=width))
    letterforms = {
        letter: os.listdir(os.path.join(fonts_path, letter))
        for letter in ALPHABET
    }

    arglists = []
    for p in possibilities:
        for n in range(variants):
            arglists.append([
                os.path.join(candidate_path, f"candidate-{''.join(p)}-{n}.png")
            ] + [
                os.path.join(fonts_path, c, random.choice(letterforms[c]))
                for c in p
            ])

    args = " ".join(f"{{{d+2}}}" for d in range(width))
    with pygpar.PP(f"montage {args} -tile {width}x1 -geometry +0+0 {{{1}}}",
                   filter_exists=True,
                   eta=True,
                   jobs=40) as _p:
        _p.queue_list(arglists)

    return next(iter(zip(*arglists)))
Example #3
0
def make_words(font_dir, out_dir, words, number, pad_char="0", pad_blank=False, random_words=0, cores=8):
    letterforms = { c: [
        os.path.join(font_dir, c, f) for f in os.listdir(os.path.join(font_dir, c))
    ] for c in os.listdir(font_dir) }

    lengths = set(len(w) for w in words)
    length = max(lengths)
    if len(lengths) > 1:
        assert pad_char is not None, "all words need to be the same length, or a pad byte should be specified"
        words = [ word.ljust(length, pad_char) for word in words ]

    if random_words:
        all_words = sorted({ w.strip().lower() for w in open('/usr/share/dict/american-english') if re.match('^[a-z]*$', w.lower()) })

    for _ in range(random_words):
        s = ""
        while len(s) < length:
            s += random.choice(all_words)
        words.append(s[:length])

    arglists = [ ]
    for word in words:
        word_dir = os.path.join(out_dir, word)
        with contextlib.suppress(FileExistsError):
            os.makedirs(word_dir)
        for wn in range(number):
            word_file = os.path.join(word_dir, "%d.png" % wn)
            letters = [ (c if c in letterforms else c.lower() if c.lower() in letterforms else c.upper()) for c in word ]
            letter_imgs = [ random.choice(letterforms[c]) for c in letters ]
            arglists.append([ word_file ] + letter_imgs)

    print("Executing %d jobs", len(arglists))
    with pygpar.PP(
        'montage ' + ' '.join('{%d}'%n for n in range(2, length+2)) + ' -tile ' + str(length) + 'x1 -geometry +0+0 {1}',
        filter_exists=True, eta=True, jobs=cores
    ) as _p:
        _p.queue_list(arglists)
    if pad_blank:
        with pygpar.PP(
            'mogrify -background black -extent 448x28 {1}',
            filter_exists=False, eta=True, jobs=cores
        ) as _p:
            _p.queue_list(arglists)
Example #4
0
def make_bullshit(font_dir, out_dir, number, cores=8):
    letterforms = { c: [
        os.path.join(font_dir, c, f) for f in os.listdir(os.path.join(font_dir, c))
    ] for c in os.listdir(font_dir) }

    words = []
    for _ in range(number):
        word = []
        for __ in range(16):
            word.append(random.choice('0123456789ABCDEF'))
        if random.randint(1,2) == 1:
            for __ in range(random.randint(1,16)):
                word[random.randint(0,15)] = '_'
        else:
            for __ in range(random.randint(1,3)):
                start, stop = random.randint(0,15), random.randint(0,16)
                if stop < start:
                    tmp = start
                    start = stop
                    stop = tmp
                word[start:stop] = '_' * (stop-start)
        words.append(''.join(word))

    arglists = [ ]
    for word in words:
        word_dir = os.path.join(out_dir, 'bullshit')
        with contextlib.suppress(FileExistsError):
            os.makedirs(word_dir)
        word_file = os.path.join(word_dir, word + ".png")
        letters = [ (c if c in letterforms else c.lower() if c.lower() in letterforms else c.upper()) for c in word ]
        letter_imgs = [ random.choice(letterforms[c]) for c in letters ]
        arglists.append([ word_file ] + letter_imgs)

    print("Executing %d jobs", len(arglists))
    with pygpar.PP(
        'montage ' + ' '.join('{%d}'%n for n in range(2, 16+2)) + ' -tile 16x1 -geometry +0+0 {1}',
        filter_exists=True, eta=True, jobs=cores
    ) as _p:
        _p.queue_list(arglists)