Skip to content

Instantly share code, notes, and snippets.

@bodqhrohro
Created July 23, 2023 11:38
Show Gist options
  • Save bodqhrohro/d145e01781506fc34c1899a8afe5c413 to your computer and use it in GitHub Desktop.
Save bodqhrohro/d145e01781506fc34c1899a8afe5c413 to your computer and use it in GitHub Desktop.
Puzzle generator
#!/bin/sh
resolution=$(identify -format "%wx%h" "$1[0]")
IFS=x read -r -a resolution_array <<< $resolution
width=${resolution_array[0]}
height=${resolution_array[1]}
WIDTH_FACTOR=15
HEIGHT_FACTOR=15
LAST_TILE_X=$(( WIDTH_FACTOR-1 ))
LAST_TILE_Y=$(( HEIGHT_FACTOR-1 ))
IMAGE_PAPER_WIDTH=277
TILE_PAPER_WIDTH=$(( IMAGE_PAPER_WIDTH / WIDTH_FACTOR ))
(( tile_width=(width+WIDTH_FACTOR-1)/WIDTH_FACTOR ))
(( tile_height=(height+HEIGHT_FACTOR-1)/HEIGHT_FACTOR ))
tmpdir="$(mktemp -d --tmpdir puzzle.XXXXXXXXXX)"
cd "$tmpdir"
for i in $(seq 0 $(( LAST_TILE_Y )))
do
offset_y=$(( tile_height*i ))
if [[ i -eq LAST_TILE_Y ]]
then
current_tile_height=$(( height-offset_y ))
else
current_tile_height="$tile_height"
fi
for j in $(seq 0 $(( LAST_TILE_X )))
do
offset_x=$(( tile_width*j ))
if [[ j -eq LAST_TILE_X ]]
then
current_tile_width=$(( width-offset_x ))
else
current_tile_width="$tile_width"
fi
convert "$1[0]" -crop "${current_tile_width}x${current_tile_height}+${offset_x}+${offset_y}" "tile-$(printf "%3.3d" $(( i*WIDTH_FACTOR+j )))-$current_tile_width-$current_tile_height.png"
done
done
papername="paper.tex"
cat > "$papername" <<-LATEXDOC
\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{lscape}
\usepackage[left=1cm, top=1cm, right=1cm, bottom=1cm]{geometry}
\makeatletter
\def\@oddfoot{}
\makeatother
\arrayrulewidth=0pt
\begin{document}
\begin{landscape}
\noindent
LATEXDOC
echo '\begin{tabularx}{'"$IMAGE_PAPER_WIDTH"'mm}{*{'"$WIDTH_FACTOR"'}{|X}|}\hline' >> "$papername"
tile_files=($(find -name 'tile-*.png'|shuf))
for i in $(seq 0 $(( LAST_TILE_Y )))
do
for j in $(seq 0 $(( LAST_TILE_X )))
do
current_tile_filename=${tile_files[$(( i*WIDTH_FACTOR+j ))]}
IFS=- read -r -a current_tile_filename_parts <<< $current_tile_filename
current_tile_width=${current_tile_filename_parts[2]}
current_tile_paper_width=$(( TILE_PAPER_WIDTH*current_tile_width/tile_width ))
echo -n '\includegraphics[width='"$current_tile_paper_width"'mm]{'"$current_tile_filename"'} ' >> "$papername"
if [[ j -eq LAST_TILE_X ]]
then
echo '\\ \hline' >> "$papername"
else
echo '&' >> "$papername"
fi
done
done
cat >> "$papername" <<-LATEXDOC
\end{tabularx}
\end{landscape}
\end{document}
LATEXDOC
pdflatex "$papername"
cd -
mv "$tmpdir/paper.pdf" "$2"
rm -r "$tmpdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment