You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
puzzle-retro/pieces-background.ipynb

2.4 KiB

In [22]:
# generate random ID for the pieces
from shortuuid import uuid

# to draw the background
from wand.image import Image
from wand.drawing import Drawing

# rows and columns for the puzzle
rows = 10
columns = 15

# resolution for the print
dpi = 300

# width and height in cm
image_width = 15
image_height = 10

# name of the puzzle to be more legible?
name = 'katamari'


# width and height in pixel
width = int(dpi / 2.54 * image_width)
height = int(dpi / 2.54 * image_height)

# piece sizes in pixel
piece_width = width / rows
piece_height = height / columns


pieces = {}

with Image(width = width, height = height) as img:
    draw = Drawing()
    draw.font_size = 9
    draw.text_alignment = 'center'
    
    for y in range(columns):
        for x in range(rows):
            
            # generate a random ID for each piece    
            ID = uuid()[:4]
            
            # write the id in the center of each piece
            draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\n{ID}')
            
            # store the id and the position in the pieces dictionary
            pieces[ID] = (x,y)
            
    draw(img)
    img.save(filename=f'{name}_retro.png')
    print('Done!')
Done!