Showing posts with label pragmatic. Show all posts
Showing posts with label pragmatic. Show all posts

Monday, July 2, 2012

Miniature Photo Album with ImageMagick (montage, convert) and Some Shell Scripting

How to develop photos smaller than 13x9, 15x11...

The fork is just there to indicate the size of the photos
When ordering photos online, you usually have the option to choose between formats like 13x9cm, 15x11cm, etc. However, none of these digital photo online services (that I know) offers smaller sizes. This post describes how to easily "merge" 4 JPG files into a big one (containing these 4 files in a 2x2-grid), which you can then order online and cut with scissors (or your other favorite cutting device) in order to create an album with photos of the size 7.5x5.5cm (or smaller, by adjusting some parameters)

Important: The shell script shown below does not touch the original files (it copies and rotates them). However, I don't give any warranty that you don't lose or ruin your original files, so make sure to have a backup!

Prerequisites

  • A set of JPEG images (optimally the same aspect ratio in order to get a seamless montage)
  • An installation of ImageMagick (the executables have to be in the $PATH)
  • A bash-terminal

How to do it

  • Move all the JPG files in a directory
  • cd into this directory and create an executable script with the content from below
  • Execute the script and wait
  • After that, the subdirectory generated contains the files you might want to upload to your online photo service  


#!/bin/bash

g=generated
t=$g/tmp

mkdir -p $t

### STEP 1 - cloning/rotating the file to 'portrait' 
### into the temporary subdirectory(0.jpg, 1.jpg, etc.)
i=0
IFS=$'\n'
for file in `find . -type f -iname "*.jp*g"`
do
 echo "rotating file $file into $t/$i.jpg"
 convert "$file" -rotate '90>' $t/$i.jpg

 let i=i+1
done


### STEP 2 - take 4 JPG files at a time 
### and compile them into new 2x2-images 
i=0
nroffiles=`ls $t/*.jpg | wc -w`
let nrofiterations=nroffiles/4
while [ $i -lt $nrofiterations ] 
do
 ## helper variables to access file names
 let f=i*4
 let a=f+0
 let b=f+1
 let c=f+2
 let d=f+3
 echo "creating a$i.jpg ($a.jpg $b.jpg $c.jpg $d.jpg)"

 ## compile the 4 separate images into a 2x2-grid. 
 ## please change the parameter 768x1024 if you 
 ## don't have a 3:4 aspect ratio
 montage $t/$a.jpg $t/$b.jpg $t/$c.jpg $t/$d.jpg -tile 2x2 -geometry 768x1024 $g/a$i.jpg
 let i=i+1
done

### remove the tmp directory
rm -rf $t



The images created by this script look somewhat like this one (individual pictures taken from http://www.freeimages.co.uk/)


Not very advanced stuff, I know, but I think it's a nice combination of some handy tools to make a nice present.

P.S. 1: I know that there are more elegant ways to write the shell-script, but for my purposes and skills this was the quickest way to go.

P.S. 2:  I think the 7.5x5.5cm format is optimal for a flip book. Wouldn't it be easy and fun to create your own flip book story with the continuous shooting mode of your digital camera?

Tuesday, January 25, 2011

Pragmatic Diary with LaTeX

A while ago, I decided to write something like a personal diary, and wanted to do it with LaTeX. I wanted to have a pragmatic environment, which is established when typing a simple command.

Before I describe the implementation, I would like to describe this environment:
  • When I run the command diary, an instance of kate opens a file named with the current date (e.g. 2010-12-09.tex, located in a directory, let's say $DIARY)
    • If this file exists, then a warning appears, and it is opened
    • Otherwise, this file is created
  • This file contains a skeleton for a LaTeX document, where I immediately can start writing down my thoughts
  • In the terminal-area of kate, I simply have to type pdflatex 2010-12-09.tex to compile the tex-file into a PDF.

And here is how I implemented it.
The template for the LaTeX-skeleton looks as follows (file named $DIARY/template.tex):

\input{./macros.tex}
\begin{document}
\title{}
\maketitle

Here goes the text
\end{document}

The file $DIARY/macros.tex contains document and package definitions, that define the common properties for all tex-files, and hence a unique look for all resulting pdf-files.
Example:

\documentclass[paper=a5,pagesize=auto]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{listings}

The script, that launches when typing diary is the following ($DIARY/diary.sh):

#!/bin/bash
TODAY=$(date +"%Y-%m-%d")
BASE=~/Dokumente/texs
TEXFILE=$BASE/$TODAY.tex
PDFFILE=$BASE/$TODAY.pdf
if [ -f $TEXFILE ] 
then
  zenity --error --text "Tex-File $TEXFILE already exists!!!"
else
  cp $BASE/template.tex $TEXFILE
fi

kate $TEXFILE &
cd $BASE
pdflatex $TEXFILE
okular $PDFFILE &

Some notes:
  • zenity simply pops up a dialog box with the given warning
  • okular is my favorite PDF viewer - in fact it could be any other viewer
Finally, I made a softlink to $DIARY/diary.sh in /usr/bin.

By the end of each year I might create a document named diary-201x.pdf, which includes all entries and is a nice compilation of my diary entries. For instance, this could be done with the pdftk. (pdftk *.pdf cat output diary-201x.pdf)

P.S.: You may wonder, why I'm mixing Gnome and KDE software. I'm working with Ubuntu and have some KDE-packages installed. Well, that's my environment, and I'm very fine with that. If you are Ubuntu-User, too, you will find all of the necessary software in the Ubuntu repositories (sudo apt-get install XXX, whereas XXX stands for kate, okular, zenity, pdftkm, etc.)