Skip to content

Instantly share code, notes, and snippets.

@Huud
Last active February 26, 2020 14:28
Show Gist options
  • Save Huud/ea81e05128d9593f26c52311c5d03354 to your computer and use it in GitHub Desktop.
Save Huud/ea81e05128d9593f26c52311c5d03354 to your computer and use it in GitHub Desktop.
A Python script that takes a B&W image and generates an illusion where lines bending create that image, it's based on a viral image that reads "I Can't Sleep", examples: https://imgur.com/a/1NT0BIe
from PIL import *
# from IPython.display import display # uncomment if you are using Jupyter
im = Image.open(r"C:\Put_your_B&W_img_here.jpg")
outIm = Image.new('RGB', im.size, color = 'white')
draw = ImageDraw.Draw(outIm)
gridSpacing = 5
lineThickness = int(gridSpacing/2)
for i in range(0,im.size[0],gridSpacing):
for j in range(0,im.size[1],gridSpacing):
draw.line((i, j, i+1, j-1), fill=0, width=lineThickness)
r,_,_ = im.getpixel((i, j))
if r > 125:
draw.line((i-1, j-1, i+gridSpacing, j+gridSpacing), fill=0, width=lineThickness)
else:
draw.line((i+5, j, i-gridSpacing+4, j+gridSpacing+1), fill=0, width=lineThickness)
outIm.show()
# display(outIm) # use this instead if you are using Jupyter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment