You are here: Home / Journal / 3D Anaglyphs

3D Anaglyphs

This article documents my first experiments in producing red-cyan anaglyphs. The gallery contains of four near-identical 3D anaglyphs with varying amounts of X & Y offset to compensate for the fact that the cameras on the board don’t quite point in the same direction. One can create anaglyphs in GIMP. But it is a tedious task because one has to do them one at a time. So it seemed sensible to write a script that would perform the bulk of the most tedious tasks automatically.

Gallery

Useful shell scripts

These BASH scripts will enable you to convert 1200 pixel x 1600 pixel stereo pairs such as those produced by the aforementioned ELP board into red cyan single image anaglyphs. These assume you are using a recent Debian GNU/Linux or one of its many derivatives (Ubuntu, KDE Neon, Kubuntu, Mint et al). These scripts also assume you have Imagemagick already installed on your system. If you do not have Imagemagick, then you will need to install it, by typing the following into a terminal and hitting the return key:-

sudo apt install imagemagick

Creating red-cyan anaglyphs from stereo pairs

This is a simple shell script to convert a pair of 1600 x 1200 pixel images into an anaglyph:-

#!/bin/bash

DIR="${1:-.}"
OUTDIR="$DIR/anaglyph"

mkdir -p "$OUTDIR"

for file in "$DIR"/*.{png,jpg,jpeg,webp}; do
[ -e "$file" ] || continue

base=$(basename "$file")
name="${base%.*}"

echo "Processing $base"

# Split into left/right
convert "$file" -crop 1600x1200+0+0 left.png
convert "$file" -crop 1600x1200+1600+0 right.png

# Extract channels
convert left.png -channel R -separate left_r.png
convert right.png -channel G -separate right_g.png
convert right.png -channel B -separate right_b.png

# Combine into RGB (R from left, G+B from right)
convert left_r.png right_g.png right_b.png -combine \
"$OUTDIR/${name}_anaglyph.jpg"

rm -f left.png right.png left_r.png right_g.png right_b.png

done

echo "Done."

Simply save the file as “garf-2anaglyph”, and be sure to set its permissions to executable:-

chmod +x garf-2anaglyph

Usage:-

./garf-2anaglyph /path/to/stereopair-files

Creating red-cyan anaglyphs from stereo pairs and adding an offset

This is a modified version of that script that allows one to alter the X and Y offset, in pixels. It is quite crude. You will need to make the necessary adjustments to the script to obtain the offset you require:-

#!/bin/bash

DIR="${1:-.}"
OUTDIR="$DIR/anaglyph"

mkdir -p "$OUTDIR"

# ---- TUNING ----
# Adjust the two values, XSHIFT and YSHIFT, manually, as needed
XSHIFT=-20
YSHIFT=+30

W=1600
H=1200

# ---- DON'T TOUCH anything below ----

for file in "$DIR"/*.{png,jpg,jpeg,webp}; do
[ -e "$file" ] || continue

base=$(basename "$file")
name="${base%.*}"

echo "Processing $base"

# -------------------------
# split stereo pair
# -------------------------
magick "$file" -crop ${W}x${H}+0+0 +repage left.png
magick "$file" -crop ${W}x${H}+${W}+0 +repage right.png

# -------------------------
# TRUE pixel shift (NO roll, NO distort)
# -------------------------
magick right.png \
-background black \
-extent ${W}x${H} \
-page +${XSHIFT}+${YSHIFT} \
-flatten \
right_fixed.png

# -------------------------
# channels
# -------------------------
magick left.png -channel R -separate left_r.png
magick right_fixed.png -channel G -separate right_g.png
magick right_fixed.png -channel B -separate right_b.png

# -------------------------
# combine
# -------------------------
magick left_r.png right_g.png right_b.png -combine \
"$OUTDIR/${name}_test_-20x+30y.jpeg"

# -------------------------
# cleanup
# -------------------------
rm -f left.png right.png right_fixed.png \
left_r.png right_g.png right_b.png

done

echo "Done."

Simply save the file as “garf-2anaglyph+”, and be sure to set its permissions to executable:-

chmod +x garf-2anaglyph+

Usage:-

./garf-2anaglyph+ /path/to/stereopair-files

Credits

With many thanks to my friend and model Estella Rose for dressing up in her “Dorothy Gale” costume and posing for these sample images.

Disclaimer

Scripts are provided as-is, with no warranty express or implied. Use and modify as you wish, but you do so at your own risk.

Slightly similar posts...

Leave a Reply

Your email address will not be published. Required fields are marked *