Convert a Single-Image Stereogram to a Red/Cyan Anaglyph
pamsistoaglyph converts a magic-eye picture into a red/cyan anaglyph, for anyone who can never quite defocus their eyes to see the hidden shape. The examples use pamsistoaglyph from Netpbm 11.5.2, installed here as Debian package version 2:11.05.02-1.1build1. Allow about ten minutes if the input image is already in a Netpbm format.
The route
Jump straight to the step you need, or tick off Done means at the end.
- You need: a readable stereogram in a Netpbm format such as PGM, PPM or PAM, the
netpbmtools, and red/cyan glasses. - Privileges: none. This guide only reads the input and writes a new output file.
- Caution: do not overwrite the original image while experimenting with separation values.
1. Confirm the tool and inspect the input
Check that the command is installed, then ask pamfile what it is reading. Replace stereogram.pam with the path to your own file:
$ command -v pamsistoaglyph
/usr/bin/pamsistoaglyph
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ pamfile stereogram.pam
The last command should identify a Netpbm image and show its dimensions; exact wording depends on the format and the installed pamfile. If it reports an unsupported or corrupt input, fix that first. The converter cannot recover an image that the Netpbm reader cannot parse.
Checkpoint
Record the width and height. The automatic minimum separation is 10 per cent of the image width, and the output is narrower than the input because the two eye views must overlap.
2. Create the anaglyph with automatic separation
Let the program estimate the distance between repeated views and write the result to a new file:
$ pamsistoaglyph stereogram.pam > stereogram-anaglyph.pam
pamsistoaglyph: Separation between left/right views = 42 pixels
That diagnostic is normally printed to standard error, so it stays visible while the image data goes to stereogram-anaglyph.pam. The number depends on the pattern: the program searches for repeated horizontal patterns and chooses a likely eye separation, it is not a conversion of a normal photograph into stereo.
Verify the new file before opening it:
$ pamfile stereogram-anaglyph.pam
stereogram-anaglyph.pam: PAM, 598 by 400 by 3 maxval 63
Tuple type: RGB
Your dimensions and maximum value will differ. On this installed build, a small smoke-test input produced RGB PAM output with a maximum value of 63. Treat the file type and successful exit status as the useful checks, not those particular numbers.
3. View the result and correct reversed depth
Open stereogram-anaglyph.pam in an image viewer that supports PAM, or convert it with another installed Netpbm tool if your viewer does not. Put on red/cyan glasses and check whether the subject appears to have the intended depth.
pamsistoaglyph assumes the input is a wall-eyed stereogram. If the scene appears to recede where it should project towards you, repeat the conversion with --invert:
$ pamsistoaglyph --invert stereogram.pam > stereogram-anaglyph-inverted.pam
pamsistoaglyph: Separation between left/right views = 42 pixels
$ pamfile stereogram-anaglyph-inverted.pam
Tip
Compare the two output files through the glasses. This is a reversible file-generation choice: delete neither file until you have selected the version that looks correct. If you do need to remove a test output, check its exact name first and remove only that generated file, never the source stereogram.
4. Supply a separation when automatic detection is wrong
Automatic detection can be confused by a low-quality or unusually coloured pattern. If the repeated background spacing is known, provide it in pixels with --sep:
$ pamsistoaglyph --sep=42 stereogram.pam > stereogram-sep42.pam
pamsistoaglyph: Separation between left/right views = 42 pixels
The value is the distance between the left and right views, not the width of the image and not a percentage. Use a value that is plausible for the actual repeating pattern. The option accepts either an equals sign or whitespace, so --sep 42 is equivalent. Long option names are safest in scripts, although the installed manual allows unique abbreviations.
If you only know a lower bound, use --minsep instead:
$ pamsistoaglyph --minsep=42 stereogram.pam > stereogram-minsep42.pam
This does not force a 42-pixel separation. It tells the detector to accept only a larger separation. With no option, the minimum is 10 per cent of the input width, rounded according to the program's image calculations.
5. Reduce colour noise during detection
For a JPEG-derived or subtly varied pattern, limit the grey levels used while looking for repeated pixels:
$ pamsistoaglyph --gray=16 stereogram.pam > stereogram-gray16.pam
pamsistoaglyph: Separation between left/right views = 42 pixels
--gray affects the search for separation; it does not mean the final output is a greyscale image, the output remains an RGB anaglyph. The documented default is 63 grey levels. Start with the default, then try a lower value when small colour variations are causing a visibly implausible separation. Combining options is allowed:
$ pamsistoaglyph --gray=16 --minsep=42 --invert stereogram.pam > stereogram-adjusted.pam
If the command exits non-zero or prints an error saying it cannot find a separation, inspect the source dimensions and pattern first. Lowering the threshold repeatedly is not a substitute for a valid stereogram: try a known --sep only when you can justify its pixel value.
Done means
- pamsistoaglyph installed and its Netpbm package version known.
- Source verified with
pamfilebefore conversion. - New PAM RGB file created and verified without overwriting the source.
- Depth checked through red/cyan glasses.
--invert,--sep,--minsepand--grayused only for the specific failure they address.