Skip to content

Instantly share code, notes, and snippets.

@avelican
Created May 7, 2023 18:28
Show Gist options
  • Save avelican/a5ee3d55cf0bcdb8d6d096bff442958f to your computer and use it in GitHub Desktop.
Save avelican/a5ee3d55cf0bcdb8d6d096bff442958f to your computer and use it in GitHub Desktop.
autostereogram generator
# credit goes to https://github.com/synesthesiam/magicpy
# Instructions: Texture image MUST be as tall as depth map (taller should work too),
# and 1/8th of its width (or thereabouts -- any width will work but might not look too good).
from PIL import Image
if __name__ == '__main__':
depth_file = "depth.jpg"
output_file = "out.jpg"
pattern_file = "tex.png"
pattern_div = 8
depth_map = Image.open(depth_file).convert("L")
depth_data = depth_map.load()
out_img = Image.new("RGB", depth_map.size)
out_data = out_img.load()
pat_img = Image.open(pattern_file).convert("RGB")
pat_data = pat_img.load()
pattern_width = pat_img.size[0]
# Create stereogram
for x in range(0, depth_map.size[0]):
for y in range(0, depth_map.size[1]):
if x < pattern_width:
out_data[x, y] = pat_data[x, y]
else:
shift = depth_data[x, y] / pattern_div # 255 is closest
out_data[x, y] = out_data[x - pattern_width + shift, y]
out_img.save(output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment