File size: 2,290 Bytes
da9ee29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1138b5
da9ee29
c1138b5
da9ee29
 
 
 
6117c93
da9ee29
c1138b5
da9ee29
 
 
 
c1138b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os

from pylatex import (
    Document,
    Command,
    Figure,
    SubFigure,
    NoEscape,
    Package,
)
from pylatex.base_classes import Environment
import glob
import random
import uuid


class Markdown(Environment):
    """A class to wrap LaTeX's markdown environment."""

    packages = [Package('markdown', ['hashEnumerators', 'smartEllipses'])]
    escape = False
    content_separator = "\n"


def generate_shuffled_images(dirname, num_rows, num_cols, document_options, geometry_options, output_dir):
    images = glob.glob(f'{dirname}/*')

    assert len(images) >= num_rows * num_cols, 'The number of images of images is less than the expected table.'

    random.shuffle(images)

    # Document default filename
    filename = f'{output_dir}/{uuid.uuid4()}'
    
    # Document object
    doc = Document(filename, geometry_options=geometry_options)

    doc.preamble.append(Command("title", document_options['title']))
    doc.preamble.append(Command("author", document_options['author']))
    doc.preamble.append(Command("date", NoEscape(r"\today")))
    doc.append(NoEscape(r"\maketitle"))

    with doc.create(Markdown()):
            doc.append(document_options['markdown'])
    print(num_rows, num_cols)

    with doc.create(Figure(position='hbt!')):
            doc.append(Command('centering'))
            for i in range(num_rows):
                for j in range(num_cols):
                    with doc.create(SubFigure(position='t', width=NoEscape(f"{1/(num_cols + 1)}\\linewidth"))) as fig:
                        fig.add_image(images[i * num_cols + j], width=NoEscape(r"\linewidth"))
                        fig.add_caption('')
                doc.append(NoEscape(r'\hspace{\fill}'))
    
    doc.generate_pdf(compiler_args=['--shell-escape'])
    doc.generate_tex()

    return filename

if __name__ == '__main__':
    dirname = os.path.abspath('./downloads/trainingSample/')
    num_rows = 5
    num_cols = 5
    document_options = {
        'title' : 'The document title',
        'author': 'The author name',
        'markdown': '# First section',
    }
    geometry_options = {"rmargin": "1cm", "lmargin": "1cm"}
    output_dir = 'build'
    generate_shuffled_images(dirname, num_rows, num_cols, document_options, geometry_options, output_dir)