Skip to content

Instantly share code, notes, and snippets.

@ageron
Created March 11, 2019 07:06
Show Gist options
  • Save ageron/e7813bb274076613e5c7b13f9e636afd to your computer and use it in GitHub Desktop.
Save ageron/e7813bb274076613e5c7b13f9e636afd to your computer and use it in GitHub Desktop.
Converts one or more 3D images for red/blue 3D glasses into stereogram images
"""
Converts one or more 3D images for red/blue 3D glasses into stereogram images. Example:
Download:
https://www.nasa.gov/sites/default/files/styles/full_width/public/thumbnails/image/nh-ut_stereo_bluered_030619.png
Then run:
python stereogram.py nh-ut_stereo_bluered_030619.png
This will generate:
nh-ut_stereo_bluered_030619_stereo.png
Run the following command for more options:
python stereogram.py --help
"""
import sys
import numpy as np
import argparse
from imageio import imread, imwrite
def convert_to_stereogram(filepath, left_channel=0, right_channel=1,
brightness=0, hpad=50, vpad=100, repeat=3,
extension="png"):
img = imread(filepath)[:, :, :] # drop alpha channel, if any
img = img.astype(np.float)
left = img[:, :, left_channel]
right = img[:, :, right_channel]
if brightness:
left_brightness = left.mean()
right_brightness = right.mean()
if left_brightness:
left *= 255. * brightness / left_brightness
left[left > 255.] = 255.
if right_brightness:
right *= 255. * brightness / right_brightness
right[right > 255.] = 255.
height = img.shape[0] + 2 * vpad
width = 2 * repeat * img.shape[1] + 2 * hpad
stereo = np.zeros((height, width), dtype=np.uint8)
stereo[vpad:-vpad, hpad:-hpad] = np.concatenate(
[left] * repeat + [right] * repeat, axis=1
)
stereo_filepath = filepath.rsplit(".", 1)[0] + "_stereo." + extension
imwrite(stereo_filepath, stereo)
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('image_paths', metavar="IMAGEPATH", nargs='+',
help='3D image file path')
parser.add_argument('--left_channel', type=int, default=0,
help='Color channel for the left view')
parser.add_argument('--right_channel', type=int, default=1,
help='Color channel for the right view')
parser.add_argument('--brightness', type=float, default=0.,
help='Set the brightness level')
parser.add_argument('--hpad', type=int, default=50,
help='Horizontal padding')
parser.add_argument('--vpad', type=int, default=100,
help='Vertical padding')
parser.add_argument('--repeat', type=int, default=3,
help='Repetitions')
parser.add_argument('--extension', default="png",
help='Stereo file extension')
parser.add_argument('--verbose', '-v', action='count', default=0)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
for filepath in args.image_paths:
if args.verbose:
print("Converting", filepath)
try:
convert_to_stereogram(filepath,
left_channel=args.left_channel,
right_channel=args.right_channel,
brightness=args.brightness,
hpad=args.hpad,
vpad=args.vpad,
repeat=args.repeat,
extension=args.extension)
except Exception as ex:
print("Error:", ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment